golang, How to decode json array's items in simplejson?

huangapple go评论91阅读模式
英文:

golang, How to decode json array's items in simplejson?

问题

您好!根据您提供的代码和要求,以下是翻译好的内容:

传入的字符串是:

{"status_list":[
    {"m_id":70,"m_status":"OK","m_status_code":200,"reported":false},
    {"m_id":71,"m_status":"Send Message Over Time","m_status_code":800,"reported":false},
    {"m_id":72,"m_status":"OK","m_status_code":200,"reported":false},
    {"m_id":73,"m_status":"OK","m_status_code":200,"reported":false}
]}

我如何获取最后一个状态元素的 m_status 字段?

我目前使用的方法是:

import github.com/bitly/go-simplejson"

....
jsonRequest, _ := simplejson.NewJson([]byte(incommingString))
mArray := jsonRequest.Get("status_list").MustArray()
mItem := mArray[3]
fmt.printf("mItem: %")
m3StatusCode := mItem["m_status_code"]   //<---<< 无法编译

我得到的错误信息是:

invalid operation: mItem["m_status_code"] (type interface {} does not support indexing)

如果我删除最后一行代码,我可以打印出 mItem 的内容:

mItem: : map[m_id:73 m_status:OK m_status_code:200 reported:%!s(bool=false)]

问题:
如何获取 m_status_code 的值?

英文:

incoming String is:

     {"status_list":[
         {"m_id":70,"m_status":"OK","m_status_code":200,"reported":false},
         {"m_id":71,"m_status":"Send Message Over Time","m_status_code":800,"reported":false},
         {"m_id":72,"m_status":"OK","m_status_code":200,"reported":false},
         {"m_id":73,"m_status":"OK","m_status_code":200,"reported":false}
         ]
      }

How can I get the m_status field of the last status element?

The way I am using is

import github.com/bitly/go-simplejson"

....
jsonRequest, _ := simplejson.NewJson([]byte(incommingString))
mArray := jsonRequest.Get("status_list").MustArray()
mItem := mArray[3]
fmt.printf("mItem: %")
m3StatusCode := mItem["m_status_code"]   //<---<< Can't compile

I got:

invalid operation: mItem["m_status_code"] (type interface {} does not support indexing)

If I remove last line code. I can print out mItem as

mItem: : map[m_id:73 m_status:OK m_status_code:200 reported:%!s(bool=false)]

QUESTION:
How can I fetch m_status_code value?

答案1

得分: 3

你需要先进行类型断言(type assertion)。

m, ok := mItem.(map[string]interface{})
if !ok {
    fmt.Println("无效的数据")
}

fmt.Println(m["m_status_code"])

这是因为mItem实际上不是一个map,而是一个interface{}。simplejson的MustArray方法返回一个[]interface{}

所以你需要断言mItem中存储的值是你可以在m_status_code上使用的类型。

英文:

You will have to do a type assertion first.

m, ok := mItem.(map[string]interface{})
if(!ok){
    fmt.Println("Invalide data")
}

fmt.Println(m["m_status_code"])

This is because mItem is not actually a map. Its an interface{}. simplejson's MustArray returns a []interface{}.

So you have to assert that the value stored in mItem is of a type you can use an index m_status_code on.

答案2

得分: 0

// 1. 转换为数组
array, err := json.Get("status_list").Array()
if err != nil {
panic(err)
}
// 2. 迭代
for _, item := range array {
// 3. 断言
if m, ok := item.(map[string]interface{}); ok {
fmt.Println(m["m_status_code"])
}
}

英文:
// 1. convert to array
array, err := json.Get("status_list").Array()
if err != nil {
	panic(err)
}
// 2. iterate
for _, item := range array {
    // 3. assertion
	if m, ok := item.(map[string]interface{}); ok {
		fmt.Println(m["m_status_code"])
	}
}

huangapple
  • 本文由 发表于 2016年3月31日 13:32:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/36324911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定