如何访问json解码上的接口字段? [英] How to access interface fields on json decode?

查看:98
本文介绍了如何访问json解码上的接口字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json文档,我使用的是一个在接口(而不是struct)中解码文档的客户端,如下所示:

  var jsonR接口{} 
err = json.Unmarshal(res,& jsonR)

如何访问接口字段?我已阅读go doc文档和博客,但我的头仍然无法获取。他们的例子似乎只显示你可以解码接口中的json,但不能解释如何使用它的字段。



我尝试使用范围循环,但当我到达地图[字符串]界面时,故事似乎结束。我需要的字段似乎在界面中。

 对于k,v:=范围jsonR {
如果k ==topfield{
fmt .Printf(k是%v,v是%v,k,v)

}

}
pre>

解决方案

像这样解析json会非常困难。解析的默认类型是 map [string] interface {} 。当主json中有另一个复杂的数据结构时(如另一个列表或对象),问题就会出现。解码json的最好方法是定义一个保存数据的结构。这些值不仅是正确的类型,而且可以提取您实际关心的特定数据。



你的结构可以像这样:

  type Top struct { 
Topfield int`json:topfield`
}

可以解码如下:

  a:= [] byte(`{topfield:123}`)
var数据Top
json.Unmarshal(a,& data)//将json解析为数据



<现在您可以使用常规的结构操作来访问您的数据,如下所示:

  value:= data.Topfield 



包含更复杂数据的json也可以被easyli解码。也许你的数据在某个地方有一个列表,你可以使用如下结构来提取它:

  type Data struct { 
States [] string`json:states`
PopulationData []国家`json:popdata`
}

类型国家结构{
Id int`json:id`
LastCensusPop int`json:lcensuspopulation`
Gdp float64`json:gdp`
}

这样的结构不仅可以解析列表,还可以解析包含字段的对象。


I have a json document and I'm using a client which decodes the document in an interface (instead of struct) as below:

var jsonR interface{}
err = json.Unmarshal(res, &jsonR)

How can I access the interface fields? I've read the go doc and blog but my head still can't get it. Their example seem to show only that you can decode the json in an interface but doesn't explain how its fields can be used.

I've tried to use a range loop but it seems the story ends when I reach a map[string]interface. The fields that I need seem to be in the interface.

for k, v := range jsonR {
    if k == "topfield" {
        fmt.Printf("k  is %v, v is %v", k, v)

    }

}

解决方案

Parsing json like this can be very difficult. The default type of a parse is map[string]interface{}. The Problem arises when you have another complex data structure within the main json(like another list or object). The best way to go about decoding json is defining a struct to hold data. Not only will the values be of the correct type but you can extract the specific data you actually care about.

Your struct can look like this:

type Top struct {
    Topfield int `json:"topfield"`
}

which can be decoded like this:

a := []byte(`{"topfield": 123}`)
var data Top
json.Unmarshal(a, &data) //parse the json into data

now you can use regular struct operations to access your data like this:

value := data.Topfield

json which contains more complex data can also be easyli decoded. Perhaps you have a list in your data somewhere, you can use a struct like the following to extract it:

type Data struct {
    States []string `json:"states"`
    PopulationData []Country `json:"popdata"`
}

type Country struct {
    Id int `json:"id"`
    LastCensusPop int `json:"lcensuspopulation"`
    Gdp  float64 `json:"gdp"`
}

such a structure can not only parse list but also parse objects withing fields.

这篇关于如何访问json解码上的接口字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆