//结构体SampleData中包含一个int32类型参数sample_para1、一个float64参数sample_para2以及一个
string型参数sample_para3,具体对象中的取值分别为12、6.54和一个字符串,则该结构体对象以JSON表示为如下内容。
{
"sample_part1" : 12,
"sample_part2" : 6.54,
"sample_part3" : "this is a test"
}
//嵌套结构体对象的示例如下所示。嵌套结构体NestedSampleData中包含了两个成员,一个是float64型,一个是SampleData型,其某个对象以JSON表示为如下形式。
{
"sample_para4": 12.4343,
"sample_para5": {
"sample_para1": 12,
"sample_para2": 6.54,
"sample_para3": "this is a test"
}
}
//数组类型参数的示例如下所示。一个float32型数组的示例如表 68所示,一个SampleData类型数组示例如表 69所示,一个NestedSampleData类型数组示例如表 70所示。其中,键名为数组参数的名称
//float32数组
{
"sample_array_": [
1.0,
2.0,
3.0
]
}
//结构体数组
{
"struct_array_sample_": [
{
"sample_para1": 12,
"sample_para2": 6.54,
"sample_para3": "this is a test"
},
{
"sample_para1": 13,
"sample_para2": 623.3254,
"sample_para3": "this is a test! this is a test!"
}
],
}
//嵌套结构体数组
{
"nested_struct_array_sample_": [
{
"sample_para4": 12.4343,
"sample_para5": {
"sample_para1": 12,
"sample_para2": 6.54,
"sample_para3": "this is a test"
}
},
{
"sample_para4": 12.4343,
"sample_para5": {
"sample_para1": 12,
"sample_para2": 6.54,
"sample_para3": "this is a test"
}
}
]
}
//哈希表类型参数的示例如下所示,哈希表对象中的每个键值对以一个两元素的JSON数组对象表示,其中第一个元素记录键的信息,第二个元素记录值的信息。键值类型均为int32的哈希表类型对象JSON格式如表 71所示,键值类型分别为string和float32的哈希表类型对象JSON格式如表 72所示。
// map<int32,int32>哈希表对象
{
"map_sample_": [
[1, 10],
[2, 20],
[3, 30]
]
}
//map<string,float32>哈希表对象
{
"map_sample_": [
["student_a", 95.0],
["student_b", 68.5],
["student_c", 100.0]
]
}
JSON的使用方法
最新推荐文章于 2022-04-24 09:17:28 发布
本文介绍了如何使用JSON表示各种数据结构,包括基本的结构体SampleData,嵌套结构体NestedSampleData,以及数组和哈希表类型。示例展示了不同类型的变量如int32、float64、string如何映射到JSON对象,并展示了结构体数组和嵌套结构体数组的表示方式。此外,还详细解释了哈希表类型,包括int32到int32以及string到float32的键值对映射。
摘要由CSDN通过智能技术生成