46

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

4 Answers 4

48

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, ... ]...

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

Sign up to request clarification or add additional context in comments.

1 Comment

It's perfect one.
37

'{ } ' used for Object and '[]' is used for Array in json

Like

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


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

Comments

9

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

Comments

-1
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

1 Comment

map contains key value pair, keys are nothing but set and values are nothing but list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.