Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JSONObject

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report