解析未知格式的JSON数据

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

Unmarshal JSON data of unknown format

问题

我的JSON数据格式如下:

{
    'Math': [
        {'Student1': 100.0, 'timestamp': Timestamp('2017-06-26 15:30:00'), 'Student2': 100.0, 'Student3': 97.058823442402414},
        {'Student1': 93.877550824911907, 'timestamp': Timestamp('2017-06-26 15:31:00'), 'Student2': 100.0, 'Student5': 100.0},
        {'Student8': 100.0, 'timestamp': Timestamp('2017-06-26 15:32:00'), 'Student10': 100.0, 'Student4': 100.0}
    ],
    'English': [
        {'Student1': 100.0, 'timestamp': Timestamp('2017-06-26 15:30:00'), 'Student5': 100.0, 'Student3': 97.058823442402414},
        {'Student1': 93.877550824911907, 'timestamp': Timestamp('2017-06-26 15:31:00'), 'Student2': 100.0, 'Student5': 100.0},
        {'Student8': 100.0, 'timestamp': Timestamp('2017-06-26 15:32:00'), 'Student10': 100.0, 'Student4': 100.0}
    ]
}

对于我来说,键名是完全未知的。我只知道JSON的格式如下:

{
    SUBJECT1: [{Student_Name1: Grade, Student_Name2: Grade, ... , Student_Name3: Grade, timestamp: Timestamp(...)}],
    SUBJECT2: [{Student_Name4: Grade, Student_Name6: Grade, ... , Student_Name5: Grade, timestamp: Timestamp(...)}],
    ...
    SUBJECTN: [{Student_Name1: Grade, Student_Name6: Grade, ... , Student_Name9: Grade, timestamp: Timestamp(...)}]
}

其中,subjectsstudent_names都是未知的,可能会有所变化。

我想将其解组为一个GoLang结构体,以便将其作为JSON对象返回给前端。我的结构体应该是什么样子?这是我尝试过的,但它没有起作用。

type GradeData struct {
    Grades map[string]interface{} `json:"-"`
}

请注意,我只提供了翻译服务,不会回答关于翻译的问题。

英文:

My JSON is in the following format:

{
'Math': 
[
    {'Student1': 100.0, 'timestamp': Timestamp('2017-06-26 15:30:00'), 'Student2': 100.0, 'Student3': 97.058823442402414},
    {'Student1': 93.877550824911907, 'timestamp': Timestamp('2017-06-26 15:31:00'), 'Student2': 100.0, 'Student5': 100.0},
    {'Student8': 100.0, 'timestamp': Timestamp('2017-06-26 15:32:00'), 'Student10': 100.0, 'Student4': 100.0}
],
'English': [
    {'Student1': 100.0, 'timestamp': Timestamp('2017-06-26 15:30:00'), 'Student5': 100.0, 'Student3': 97.058823442402414},
    {'Student1': 93.877550824911907, 'timestamp': Timestamp('2017-06-26 15:31:00'), 'Student2': 100.0, 'Student5': 100.0}, 
    {'Student8': 100.0, 'timestamp': Timestamp('2017-06-26 15:32:00'), 'Student10': 100.0, 'Student4': 100.0}
]
}

The keys are completely unknown to me. All I know is that the JSON will be of the format:

{
SUBJECT1: [{Student_Name1: Grade, Student_Name2: Grade, ... , Student_Name3: Grade, timestamp: Timestamp(...)}],
SUBJECT2: [{Student_Name4: Grade, Student_Name6: Grade, ... , Student_Name5: Grade, timestamp: Timestamp(...)}]
...
SUBJECTN: [{Student_Name1: Grade, Student_Name6: Grade, ... , Student_Name9: Grade, timestamp: Timestamp(...)}]
}

where the subjects, student_names are all unknown and could vary.

I want to unmarshal this into a GoLang struct so I can return it to my front-end as a JSON object. What should my struct look like? This is what I tried, but it didn't work.

type GradeData struct {
	Grades map[string]interface{} `json:"-"`
}

答案1

得分: 11

  • 如果你不知道键的情况下,可以使用map[string]interface{}来解析你的JSON数据。
  • 如果你在struct字段上使用json:"-"标签,这些字段在JSON的编组/解组过程中将被忽略。

你可以尝试以下选项:Go Playground链接

选项1:

var grades map[string]interface{}

err := json.Unmarshal([]byte(jsonString), &grades)
fmt.Println(err)

fmt.Printf("%#v\n", grades)

选项2: 如果你想使用struct

var gradesData GradeData
err := json.Unmarshal([]byte(jsonString), &gradesData.Grades)
fmt.Println(err)

fmt.Printf("%#v\n", gradesData)
英文:
  • If you don't know the keys, you can use map[string]interface{} to unmarshal your JSON payload.
  • If you use json:"-" tag for the struct fields, those fields will be ignored during JSON Marshal/Unmarshal.

You can try following options: Go Playground link

Option 1:

var grades map[string]interface{}

err := json.Unmarshal([]byte(jsonString), &grades)
fmt.Println(err)

fmt.Printf("%#v\n", grades)

Option 2: if you want have struct

var gradesData GradeData
err := json.Unmarshal([]byte(jsonString), &gradesData.Grades)
fmt.Println(err)

fmt.Printf("%#v\n", gradesData)

huangapple
  • 本文由 发表于 2017年6月27日 13:54:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/44773082.html
匿名

发表评论

匿名网友

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

确定