dongmaonao0505 2018-02-26 22:01
浏览 52
已采纳

从JSON中的JSON数组中提取值(以及为什么不反映和编译错误报告同一类型)

I am having trouble unmarshalling some JSON in Go. In the below code, I want to be able to extract the value of "c" ("Apple", "Berry") from the JSON (<- main goal/question). What has me particularly confused is when I tried to index into the v array(?), I get the error: invalid operation: v[0] (type interface {} does not support indexing).

This is what brought me to using reflect and looking at the type of the variable, which as you can see below is "[]interface {}", even if I assert it to be "interface {}". Of note, if I make the line q := interface{}(v) into q := []interface{}(v), I get: cannot convert v (type interface {}) to type []interface {}: need type assertion. What am I missing? Why does reflect say it is "[]interface {}" but errors say it is "interface {}"? (<- secondary question / help me understand)

package main

import (
    "encoding/json"
    "fmt"
    "reflect"
)

func main() {
    data := []byte("{ \"a\" : { \"b\" : [ { \"n\" : 100, \"c\" : \"Apple\", \"m\" : 1, \"bool\" : false }, { \"n\" : 101, \"c\" : \"Berry\", \"m\" : 2, \"bool\" : false } ] }, \"foo\" : false, \"bar\" : \"foobar\" }")
    var a map[string]interface{}

    _ = json.Unmarshal(data, &a)

    b := a["a"].(map[string]interface{})
    for i, v := range b {
        fmt.Println(i, reflect.TypeOf(v))
        q := interface{}(v)
        fmt.Println(q, reflect.TypeOf(q))
    }

}

Output:

b []interface {}
[map[m:1 bool:false n:100 c:Apple] map[c:Berry m:2 bool:false n:101]] []interface {}
  • 写回答

1条回答 默认 最新

  • dqsk4643 2018-02-27 00:00
    关注

    Instead of attempting to unmarshal the json into an interface, you can create specific types to model what the data will be. See the example code below.

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    func main() {
        data := []byte("{ \"a\" : { \"b\" : [ { \"n\" : 100, \"c\" : \"Apple\", \"m\" : 1, \"bool\" : false }, { \"n\" : 101, \"c\" : \"Berry\", \"m\" : 2, \"bool\" : false } ] }, \"foo\" : false, \"bar\" : \"foobar\" }")
    
        type c struct {
            N int    `json:"n"`
            C string `json:"c"`
            M int    `json:"m"`
            B bool   `json:"bool"`
        }
        type b struct {
            C   []c    `json:"b"`
            Foo bool   `json:"foo"`
            Bar string `json:"bar"`
        }
        type a struct {
            A b `json:"a"`
        }
    
    
        var result a
        if err := json.Unmarshal(data, &result); err != nil {
            panic(err.Error())
        }
    
        fmt.Printf("%+v", result)
    }
    

    Link to playground.

    output: {A:{C:[{N:100 C:Apple M:1 B:false} {N:101 C:Berry M:2 B:false}] Foo:false Bar:}}

    Note that the type members are capitalised - this is required for the json unmarshaling to work correctly.

    Once the json is unmarshaled into your types, you can refer to the members as you'd expect:

        for _, v := range result.A.C {
            fmt.Println(v)
        }
    

    output:

    {100 Apple 1 false}
    {101 Berry 2 false}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c