JSON 数组结构变化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14130245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:41:42  来源:igfitidea点击:

JSON Array Structure Variations

json

提问by MikeS

Below are 3 JSON Array structure formats...

以下是 3 种 JSON 数组结构格式...

The first one, the one outlined at JSON.org, is the one I am familiar with:

第一个,JSON.org 上概述的,是我熟悉的:

Format #1

格式#1

{"People": [
  {
    "name": "Sally",
    "age": "10"
  },
  {
    "name": "Greg",
    "age": "10"
  }
]}

The second one is a slight variation that names the elements of the array. I personally don't care for it; you don't name elements of an array in code (they are accessed by index), why name them in JSON?

第二个是命名数组元素的轻微变体。我个人不关心它;你不在代码中命名数组的元素(它们通过索引访问),为什么用 JSON 命名它们?

Format #2

格式#2

{"People": [
  "Person1": {
    "name": "Sally",
    "age": "10"
  },
  "Person2": {
    "name": "Greg",
    "age": "10"
  }
]}

This last one is another variation, quite similar to Format #2, but I have a hunch this one is incorrect because it appears to have extra curly braces where they do not belong.

最后一个是另一种变体,与Format #2非常相似,但我有一种预感这是不正确的,因为它似乎在不属于它们的地方有额外的花括号。

Format #3

格式#3

{"People": [
  {
    "Person1": {
      "name": "Sally",
      "age": "10"
    }
  },
  {
    "Person2": {
      "name": "Greg",
      "age": "10"
    }
  }
]}

Again, I'm confident that Format #1is valid as it is the JSON Array format outlined at JSON.org. However, what about Format #2and Format #3? Are either of those considered valid JSON? If yes, where did those formats come from? I do not see them outlined at JSON.org or on Wikipedia.

同样,我相信格式 #1是有效的,因为它是 JSON.org 中概述的 JSON 数组格式。但是,格式 #2格式 #3呢?其中任何一个都被认为是有效的 JSON?如果是,这些格式来自哪里?我没有在 JSON.org 或 Wikipedia 上看到它们的概述。

回答by Eugen Rieck

Both #1 and #3 are (nearly - there are commas missing) valid JSON, but encode different structures:

#1 和 #3 都是(几乎 - 缺少逗号)有效的 JSON,但编码不同的结构:

  • #1 gives you an Arrayof Objects, each with name and age String properties
  • #3 gives you an Arrayof Objects, each with a single Object property, each with name and age String properties.
  • #1给你一个数组对象,每一个姓名和年龄的String类型属性
  • #3给你一个数组对象,每一个对象属性,每一个姓名和年龄String类型的属性

The #2 is invalid: Arrays (as defined by [... ]) may not contain property names.

#2 无效:数组(由[...定义])可能不包含属性名称。

回答by Muhammad Fahad

Solution For Format#1By default:

格式#1 的解决方案 默认情况下:

array=[];
object={};

JSON Code:

JSON 代码:

var Json = {
    People:[]
};
Json.People.push({
     "name": "Sally",
     "age": "10"                        
});
Json.People.push({
     "name": "Greg",
     "age": "10"                        
});

JSON Result:

JSON 结果:

{"People": [ { "name": "Sally", "age": "10" }, { "name": "Greg", "age": "10" } ] }

{"People": [ { "name": "Sally", "age": "10" }, { "name": "Greg", "age": "10" } ] }