用空字段解析json

时间:2015-09-09 11:02:23

标签: json parsing go

如何从没有字段的json解析数据? Omitempty不工作。

需要获取int 1234和其他数据(id,所有者)

{"response":[1234,{"id":1,"owner":3},{"id":3,"owner":5}]}

代码

package main

import (
    "encoding/json"
    "fmt"
)

type SectionDB_Exam_Add struct {
    id          int         `json:"id"`
    Owner           int         `json:"owner"`
}

type CreateExamType struct {
    Response []SectionDB_Exam_Add
}

var tests = `{"response":[{"id":1,"owner":3},{"id":3,"owner":5}]}`
var how_parse_this = `{"response":[1234,{"id":1,"owner":3},{"id":3,"owner":5}]}`

func main() {

    var var_CreateExamType = &CreateExamType{}

    var b_Data = []byte(tests)
    //var b_Data = []byte(how_parse_this)       // How parse 1234 and other data?

    json.Unmarshal(b_Data, var_CreateExamType)

    for _, mt := range var_CreateExamType.Response {
              fmt.Println(mt.Owner)
        }

}

游乐场: http://play.golang.org/p/NwGrrbTh-e

0 个答案:

没有答案