当前位置:网站首页>Understand the difference and use between jsonarray and jsonobject

Understand the difference and use between jsonarray and jsonobject

2022-06-26 14:30:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm your friend, Quan Jun .

One .JSONObject What is it? ? It's the object json; I knew it in seconds json Said in ,{} Representing objects , therefore JSONObject Namely :{“name”:”tom”} meanwhile , The object here Json By adding an array json Can become an array of objects json: {“name”:[“tome”,”kate”]} for example : JSONObject jsono = new JSONObject();

JSONArray jsona = new JSONArray();

jsona.put(“tom”);

jsona.put(“kate”);//[“tom”,”kate”]

jsono.put(“name”,jsona);//{“name”:[“tom”,”kate”]}

Two .JSONArray What is it? ? It's an array. json I knew it in seconds json Said in ,[] Represents an array , therefore JSONArray Namely [“tom”,”kate”,”jerry”]; perhaps [1,2,3] meanwhile , The array here json By adding objects json Can become an array object json: [{“name”:”tom”},{“name”:”kate”}] for example : JSONArray jsona = new JSONArray();

JSONObject jsono1 = new JOSNObject();

JSONObject jsono2 = new JSONObject();

jsono1.put(“name”,”tom”);//{“name”:”tom”} jsono2.put(“name”,”kate”);//{“name”:”kate”}

jsona.put(json1,json2);//[{“name”:”tom”},{“name”:”tom”}]

3、 ... and . The combination of the two can also generate object array objects json:{“book”:[{“name”:”harrypotter”},{“name”:”potterharry”}]} ① Create objects json->jsono1:{“name”:”harrypotter”}:jsona1.put(“name”,”harrypotter”); ② Create objects json->jsono2:{“name”:”potterharry”}:jsona2.put(“name”,”potterharry”); ③ Create array json->jsona:[{“name”:”harrypotter”},{“name”:”potterharry”}]:jsona.put(jsona1);jsona.put(jsona2); ④ Create objects json->jsono3:jsono3.put(“book”,jsona);

notes :JSONArray and JSONObject The first and most important difference in reuse is : JSONArray It is added one by one ; JSONObject It's one-on-one ;

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/133818.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261331048911.html