格式化 JSON 对象时“{”和“[”之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11045639/
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:23:25  来源:igfitidea点击:

Difference between '{' and '[' when formatting JSON object

json

提问by Clarence

Is there any difference between '{' and '[' when formatting a JSON object?

格式化 JSON 对象时,'{' 和 '[' 之间有什么区别吗?

采纳答案by Samy Vilar

Yep one {...}is used to define a single object, while the other [...]is used to define a sequence of either objects, values or lists ...

{...}是的,一个用于定义单个对象,而另一个[...]用于定义一系列对象、值或列表......

objects are defined as such {key:object or list or value , ...}list ares nothing more than a sequence of either objects or lists or values, [objects or list or values, ... ]...

对象被定义为这样的{key:object or list or value , ...}列表只不过是对象或列表或值的序列,[objects or list or values, ... ]......

[{'value':1}, {'values':[1,2,3,3, {'a':'a', 'b':'b'}]}, 2, 3, 4]

[{'value':1}, {'values':[1,2,3,3, {'a':'a', 'b':'b'}]}, 2, 3, 4]

回答by Jitender Mahlawat

'{ }' used for Objectand '[]' is used for Arrayin json

{ }”用于对象,“ []”用于json中的数组

Like

喜欢

var sampleObj = {
                a:1,
                b:'ab'
                };


var sampleArr = [1,'ab',4];

回答by Luchian Grigore

They don't have the same meaning at all. {}denote containers, []denote arrays.

它们根本没有相同的含义。{}表示容器,[]表示数组。

回答by Ravi Kumar

package ravi.kumar;

import java.util.ArrayList;
import java.lang.Object;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class SetListClass {
    public static void main(String[] args) {
        SetListClass SetListClass = new SetListClass();
        List<String> list = new ArrayList<String>();
        list.add("country");
        list.add("state");
        list.add("distract");
        list.add("country");
        System.out.println(list);
        System.out.println("----------------------------------------------");
        SetListClass.getset();
        System.out.println("----------------------------------------------");
        SetListClass.getHashMap();
    }
    public void getset()
    {
        Set<String> set = new HashSet<String>();
        set.add("country");
        set.add("state");
        set.add("distract");
        set.add("country");
        System.out.println(set);
        System.out.println(set.remove("country"));
        System.out.println("---------------------------------------------");
        System.out.println(set);
    }
    public void getHashMap() {
        HashMap<String, Object> hashmap = new HashMap<String, Object>();
        hashmap.put("country", "india");
        hashmap.put("state", "bihar");
        hashmap.put("district", "buxar");
        System.out.println(hashmap);
    }
}



output
-------
[country, state, distract, country] ------array
----------------------------------------------
[state, distract, country] ----array
true
---------------------------------------------
[state, distract]
----------------------------------------------
{state=bihar, district=buxar, country=india}  ---Object