Java Operation JSON data

Source: Internet
Author: User

The recent use of Java to manipulate JSON data, although JSON used a few times, but has not been well summarized, while this time to summarize the Java operation JSON data. Java operation JSON has a lot of toolkit, the address is http://www.json.org/, you can pick up yourself.

Here I use the first Org.json, the latest source address is:Https://github.com/douglascrockford/JSON-java, you can download, I will also upload in the attachment. Download the source code and put it into the project, the package is named Org.json


The JSON data we want to manipulate is as follows:

{"Total": 2, "Class": "Second Grade", "members": [{"Age": "" "," Name ":" Li Xiaohong "," Score ": 95},{" Age ": 85," name ":" Wang Nima "," score ": +/-)}


We first use the Createjson () method to generate the JSON data and then parse all the properties.

  1. Package Myclass;


  2. Import Org.json.JSONArray;

  3. Import Org.json.JSONObject;


  4. public class Test {


  5. public static void Main (string[] args) {

  6. Parse json--incoming JSON string

  7. Jsonobject my_json=new Jsonobject (Createjson ());

  8. int Total=my_json.getint ("total");

  9. System.out.println ("total:" +total);


  10. String class_name=my_json.getstring ("class");

  11. SYSTEM.OUT.PRINTLN ("Class for:" +class_name);


  12. Jsonarray Members=my_json.getjsonarray ("members");

  13. for (int i=0;i<members.length (); i++)

  14. {

  15. Jsonobject member= (jsonobject) members.get (i);

  16. System.out.println ("Name:" +member.getstring ("name") + "Age:" +member.getint ("ages")

  17. + "Score:" +member.getint ("score"));

  18. }


  19. }


  20. public static String Createjson ()

  21. {

  22. Jsonobject my_json=new jsonobject ();

  23. My_json.put ("Class", "Second Grade");

  24. My_json.put ("Total", 2);


  25. Jsonarray members=new Jsonarray ();

  26. The first classmate of the

  27. Jsonobject member1=new jsonobject ();

  28. Member1.put ("name", "Li Xiaohong");

  29. Member1.put ("Age", 18);

  30. Member1.put ("Score", 95);

  31. Members.put (Member1);

  32. A second classmate

  33. Jsonobject member2=new jsonobject ();

  34. Member2.put ("name", "Wang Nima");

  35. Member2.put ("Age", 28);

  36. Member2.put ("Score", 85);

  37. Members.put (MEMBER2);

  38. My_json.put ("members", members);


  39. return my_json.tostring ();

  40. }


  41. }

Copy Code

The result of the operation is:


Java Operation JSON data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.