Skip to content

NewDecoder error "encoding/json" is right #178

Closed
@Jiachang-Cai

Description

@Jiachang-Cai

`package main

import (
"encoding/json"
"github.com/json-iterator/go"
"strings"
"fmt"
)

const jsonStream = {"name":"xxxxx","bundle_id":"com.zonst.majiang","app_platform":"ios","app_category":"100103", "budget_day":1000,"bidding_min":1,"bidding_max":2,"bidding_type":"CPM", "freq":{"open":true,"type":"day","num":100},"speed":1, "targeting":{"vendor":{"open":true,"list":["zonst"]}, "geo_code":{"open":true,"list":["156110100"]},"app_category":{"open":true,"list":["100101"]}, "day_parting":{"open":true,"list":["100409","100410"]},"device_type":{"open":true,"list":["ipad"]}, "os_version":{"open":true,"list":[10]},"carrier":{"open":true,"list":["mobile"]}, "network":{"open":true,"list":["4G"]}},"url":{"tracking_imp_url":"http://www.baidu.com", "tracking_clk_url":"http://www.baidu.com","jump_url":"http://www.baidu.com","deep_link_url":"http://www.baidu.com"}}

func IteratorReadJsonObject(obj interface{}) error {
decoder := jsoniter.NewDecoder(strings.NewReader(jsonStream))
err := decoder.Decode(obj)
if err != nil {
return err
}
return nil
}
func ReadJsonObject(obj interface{}) error {
decoder := json.NewDecoder(strings.NewReader(jsonStream))
err := decoder.Decode(obj)
if err != nil {
return err
}
return nil
}

type IteratorObject struct {
Name *string json:"name"
BundleId *string json:"bundle_id"
AppCategory *string json:"app_category"
AppPlatform *string json:"app_platform"
BudgetDay *float32 json:"budget_day"
BiddingMax *float32 json:"bidding_max"
BiddingMin *float32 json:"bidding_min"
BiddingType *string json:"bidding_type"
Freq *jsoniter.RawMessage json:"freq"
Targeting *jsoniter.RawMessage json:"targeting"
Url *jsoniter.RawMessage json:"url"
Speed *int json:"speed" db:"speed"
}
type Object struct {
Name *string json:"name"
BundleId *string json:"bundle_id"
AppCategory *string json:"app_category"
AppPlatform *string json:"app_platform"
BudgetDay *float32 json:"budget_day"
BiddingMax *float32 json:"bidding_max"
BiddingMin *float32 json:"bidding_min"
BiddingType *string json:"bidding_type"
Freq *json.RawMessage json:"freq"
Targeting *json.RawMessage json:"targeting"
Url *json.RawMessage json:"url"
Speed *int json:"speed" db:"speed"
}

func main() {
iterator_obj, obj := &IteratorObject{}, &Object{}
IteratorReadJsonObject(iterator_obj)
ReadJsonObject(obj)
fmt.Println("iterator:" + string(*iterator_obj.Freq)) //ttp://www.baidu.com","jump_url":"htt
fmt.Println("default:" + string(*obj.Freq)) //{"open":true,"type":"day","num":100}

}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions