Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON中的匿名字段_Json_Go - Fatal编程技术网

JSON中的匿名字段

JSON中的匿名字段,json,go,Json,Go,我正在反向工程一些JSON,它似乎使用匿名字段名。例如: { "1": 123, "2": 234, "3": 345 } 顺便说一句,它并不是简单地使用1、2和3,因为它们表示的用户ID最小为int32 是否有一些方法,例如使用标记来正确地解组JSON 我试过: package main import ( "encoding/json" "fmt" ) type MyStruct struct { string `json

我正在反向工程一些JSON,它似乎使用匿名字段名。例如:

{
   "1": 123,
   "2": 234,
   "3": 345
}
顺便说一句,它并不是简单地使用1、2和3,因为它们表示的用户ID最小为int32

是否有一些方法,例如使用标记来正确地解组JSON

我试过:

package main

import (
        "encoding/json"
        "fmt"
)

type MyStruct struct {
        string `json:",string"`
}

func main() {
        jsonData := []byte("{\"1\":123,\"2\":234,\"3\":345}")

        var decoded MyStruct
        err := json.Unmarshal(jsonData, &decoded)
        if err != nil {
                panic(err)
        }
        fmt.Printf("decoded=%+v\n", decoded)
}

只需将数据解码为map[字符串]int:

然后,您将能够通过用户ID键迭代并访问元素:

for userID, _ := range decoded {
    fmt.Printf("User ID: %s\n", userID)
}
for userID, _ := range decoded {
    fmt.Printf("User ID: %s\n", userID)
}