Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use JSONObject

Shulou Source: shulou.com Published: 2022-06-02 04:47:54 09月24日 Update

This article will explain in detail how to use JSONObject for you. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

JSONObject is just a data structure, which can be understood as a data structure in JSON format (key-value structure). You can use the put method to add elements to json objects. JSONObject can be easily converted into strings, or other objects can be easily converted into JSONObject objects.

Pom:

Com.alibaba fastjson 1.2.28 1. Generate the json data format natively. JSONObject zhangsan = new JSONObject (); try {/ / add zhangsan.put ("name", "Zhang San"); zhangsan.put ("age", 18.4); zhangsan.put ("birthday", "1900-20-03"); zhangsan.put ("majar", new String [] {"", "hey"}) Zhangsan.put ("null", null); zhangsan.put ("house", false); System.out.println (zhangsan.toString ());} catch (JSONException e) {e.printStackTrace ();} 2. Generate HashMap zhangsan = new HashMap () through hashMap data structure; zhangsan.put ("name", "Zhang San"); zhangsan.put ("age", 18.4); zhangsan.put ("birthday", "1900-20-03"); zhangsan.put ("majar", new String [] {"", "hey"}); zhangsan.put ("null", null) Zhangsan.put ("house", false); System.out.println (new JSONObject (zhangsan). ToString ()); 3. Generate Student student = new Student (); student.setId (1); student.setAge ("20"); student.setName ("Zhang San") through entities; / / generate json format System.out.println (JSON.toJSON (student)); / / convert objects to string String stuString = JSONObject.toJSONString (student) Convert 4.JSON strings to JSON objects String studentString = "{\" id\ ": 1,\" age\ ": 2,\" name\ ":\" zhang\ "}"; / / JSON strings to JSON objects JSONObject jsonObject1 = JSONObject.parseObject (stuString); System.out.println (jsonObject1); 5.list objects to listJsonArrayList studentLsit = new ArrayList (); Student student1 = new Student (); student1.setId (1); student1.setAge ("20") Student1.setName ("asdasdasd"); studentLsit.add (student1); Student student2 = new Student (); student2.setId (2); student2.setAge ("20"); student2.setName ("aaaa:;aaa"); studentLsit.add (student2); / / list to json string String string = JSON.toJSON (studentLsit). ToString (); System.out.println (string) / / json string to listJson format JSONArray jsonArray = JSONObject.parseArray (string); System.out.println (jsonArray); this is the end of the article on "how to use JSONObject". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Object character string data format structure generation data structure article Zhang San more good practical element content just entity article method knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux Shulou Information MySQL Shulou Tech Info