我正在尝试在一个文档中保存多个JSON数组。我有一个格式如下的JSON数组:
[{"key":"Math","value":6},{"key":"Biology","value":"8"},{"key":"English","value":"7"},{"key":"Gym","value":"4"}]
我想在一个文件中保存这些的多个,但我得到错误:
json只允许一个顶级值
我通过格式化JSON来防止此错误,如:
[{"key":"Math","value":6},{"key":"Biology","value":"8"},{"key":"English","value":"7"},{"key":"Gym","value":"4"}], [{"key":"Math","value":6},{"key":"Biology","value":"8"},{"key":"English","value":"7"},{"key":"Gym","value":"4"}]
在JSON数组之间的','它表示:预期值,得到','。
有人可以帮帮我吗?
修改
事实证明,我想要的是不可能的。我正在尝试另一种JSON格式!感谢大家。
答案 0 :(得分:0)
您可以将它们保存到另一个阵列中:
[
[{"key":"Math","value":6}, ...],
[{"key":"Math","value":6}, ...],
[{"key":"Math","value":6}, ...],
[{"key":"Math","value":6}, ...],
...
]
所以,这样语法就是正确的,因为你将一个大数组内的数组分开,导致你在文件中保存一个数组数组。
然后,在读取文件时,您将返回一个数组数组。
答案 1 :(得分:0)
就我而言,从服务器发送的 json 缺少方括号。愚蠢的服务器。
错误:
{
"title": "this is title 1" ,
"text": "this is the text contained 1"
},
{
"title": "this is title 2",
"text": "this is the text contained 2"
}
对:
[
{
"title": "this is title 1",
"text": "this is the text contained 1"
},
{
"title": "this is title 2",
"text": "this is the text contained 2"
}
]
答案 2 :(得分:-1)
你应该把一个最重要的值放在"键"