无需元素名称解析JSON

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

Unmarshal json without elements names

问题

我正在尝试读取一个 JSON 文件并将其解析为 Go 类中的 jsonObject。
当我接收到 JSON 时,它的元素名称和数量是随机的。
例如:

{"707514313":1505680270,"1568212945":1505676950,"732898933":1505681884}

所以,我看到的所有示例都使用结构体来定义解组的接口,其中他们放置了 JSON 值的名称,但在我的情况下,我无法这样做,因为我不知道 JSON 值的数量和名称。

var settings struct {
    Name1  string `json:"707514313"`
    Name2  string `json:"1568212945"`
    还有多少个以及它们的名称?!
}

所以我最终使用默认接口进行解组

func loadFileToJson(filename string) {
    plan, _ := ioutil.ReadFile(filename)
    var data interface{}
    checkError(json.Unmarshal(plan, &data))
    fmt.Println("Data %s ", data)
}

这将在 data 中加载一个 (map[string]interface{})

有什么办法可以实现我想要的效果吗?

编辑:

我创建了这个结构体

type Structure struct {
    Name map[string]uint64
}

并将旧的默认值更改为

var jsonObject []Structure
checkError(json.Unmarshal(plan, &jsonObject))

但是出现了以下错误

json: cannot unmarshal object into Go value of type []main.Structure
Data %s  []
英文:

I´m trying to read a json file and parse into jsonObject in my Go class.
The json has a random names and number of elements when I receive it.
For example:

{"707514313":1505680270,"1568212945":1505676950,"732898933":1505681884}

So all the examples that I´ve seen that use an struct to define the interface for the unmarshal, where they put the names of the json values, but in my case I cannot do it since I dont know how many and the name of the values of the json.

var settings struct {
    Name1  string `json:"707514313"`
    Name2  string `json:"1568212945"`
    Who knows how many more and with which names?!
}

So I end up unmarshalling with the default interface

func loadFileToJson(filename string) {
	plan, _ := ioutil.ReadFile(filename)
	var data interface{}
	checkError(json.Unmarshal(plan, &data))
	fmt.Println("Data %s ", data)
}

That load in data a (map[String]interface{})

Any idea how to achieve what I want.

EDIT:

I create this struct

type Structure struct {
    Name map[string]uint64
}

And changing the old default by

var jsonObject []Structure
checkError(json.Unmarshal(plan, &jsonObject))

Is giving me this error

json: cannot unmarshal object into Go value of type []main.StructureData %s  []

答案1

得分: 3

根据@Anzel的指示,你的数据似乎非常适合使用map[string]uint64。这假设了几件事情,即你的对象键始终是字符串(就像你的示例中一样),并且值始终是uint64(正如你的示例数据所示)。因此,将其解组为该数据类型,而不是interface{}

plan := []byte(`{"707514313":1505680270,"1568212945":1505676950,"732898933":1505681884}`)
var data map[string]uint64
json.Unmarshal(plan, &data)
fmt.Printf("Data is %+v\n", data)

输出

Data is map[1568212945:1505676950 732898933:1505681884 707514313:1505680270]
英文:

As @Anzel pointed out your data appears to be perfect for a map[string]uint64. This assumes a couple things, namely that your object keys are always strings (as in your example) and that the values are always uint64 (again as your sample data suggested). As such, unmarshal into that data type instead of interface{}

plan := []byte(`{"707514313":1505680270,"1568212945":1505676950,"732898933":1505681884}`)
var data map[string]uint64
json.Unmarshal(plan, &data)
fmt.Printf("Data is %+v\n", data)

OUTPUT

Data is map[1568212945:1505676950 732898933:1505681884 707514313:1505680270]

答案2

得分: 2

如评论所述,您只需要将字段类型设置为map[string]uint64,并实现一些方法来解析文件并获取地图值。

在此播放场中查看一些伪代码:
playground

但是,根据您的地图值,您可能需要将字段类型定义为map[string]uint64或反映JSON结构的其他类型,例如map[string]interface{},甚至是具有嵌套结构的单独嵌入式结构。

希望这可以帮助到您。

英文:

As commented, you just need to set the field type as map[string]uint64 and implement a few methods to parse the file and get the map value.

See in this playground for some pseudo code:
playground

However, depending on your map values, you may need to define the field type as map[string]uint64 or whatever reflecting the json structure, e.g. map[string]interface{} or even a separate embedded struct with nested structure.

Hope this helps.

huangapple
  • 本文由 发表于 2017年9月18日 05:14:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/46268940.html
匿名

发表评论

匿名网友

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

确定