JSON parsing tool-org.json use tutorial

Source: Internet
Author: User

First, Introduction  

Org.json is a commonly used JSON parsing tool in Java, mainly providing jsonobject and Jsonarray classes, which are now explained by the use of each class .  

Second, prepare  

1. before using Org.json, we should start with the URL https://github.com/douglascrockford/ Json-java Download Org.json source code, and add the source to eclipse, you can call .  

2. View the relevant API documentation, visit:https://github.com/douglascrockford/ Json-java.  

Third, the explanation :  

1.JSONObject:  

  • is an unordered collection of key / value pairs .  

  • Its representation is a string wrapped in curly braces, separated by a colon between the key and the value, separated by a comma between the key value and the key value . Span class= "Normaltextrun SCX229584015".  

  • Intrinsic form is a use get () and opt () methods access values by key, and use put Span class= "Normaltextrun SCX229584015" > () methods use keys to add or replace objects with values  

  • values can be any of these types: Boolean,jsonarray,jsonobject,number and string, or Josonobject.null objects.  

The code demonstrates the following :  

public static void Jsonobjecttest () {jsonobject-jsonobj = new Jsonobject ("{' Name ': ' Xiazdong ', ' age ': +}");     String name = jsonobj.getstring ("name"); int age = Jsonobj.getint ("Age");     

Note: Jsonobject has many optxxx methods, such as Optboolean, optstring, optint...  

What they mean is that if the Jsonobject has this property, it returns this property, otherwise it returns a default value .  

2.JSONArray:  

  • is an ordered sequence value .  

  • It behaves as a string wrapped in square brackets, separated by commas between values and values .  

  • Intrinsic form is a use get () and opt () methods access values by index, and use put Span class= "Normaltextrun SCX229584015" > () method to add or modify the value of the object . Span class= "Normaltextrun SCX229584015".  

  • The values can be any of these types: Boolean,jsonarray,jsonobject,number, and string, or Jsonobject.null object.  

The code demonstrates the following :  

public static void Jsonarraytest () {Jsonarray Jsonarray = new Jsonarray ("[{' Name ': ' Xiazdong ', ' age ': 20},{' name ': ' Xzdong ', ' Age ': 15}] "); for (int i = 0; i < jsonarray.length (); i++) {Jsonobject jsonobj = Jsonarray.getjsonobject (i);         String name = jsonobj.getstring ("name");         int age = Jsonobj.getint ("Age");         System.out.println ("name =" + Name + ", age =" + age ");     

the nested jsonobject and Jsonarray codes are shown below :  

public static void Jsonobjectandarraytest () {String jsonstring = ' {' name ': ' Xiazdong ', ' age ': ' The ' book ': [' Book1 ', ' Book2 ' ]}"; Jsonobject jsonobj = new Jsonobject (jsonstring);      String name = jsonobj.getstring ("name"); System.out.println ("name" + ":" + name);      int age = Jsonobj.getint ("Age");     System.out.println ("Age" + ":" + age);  Jsonarray Jsonarray = Jsonobj.getjsonarray ("book"); for (int i = 0; i < jsonarray.length (); i++) {         String book = jsonarray.getstring (i); System.out.println ("book" + i + ":" + book);     }   

3.JSONStringer:  

  • is a tool for quickly constructing JSON text  

  • Subclass of Jsonwriter  

  • bject () : {; enobject (): End an object, add &NBSP;

  •  array (): Start an array, that is, add [;&NBSP; endarray (): End an array, add &NBSP;

  • Key (): indicates adding a key;value(): means adding a value  

The code demonstrates the following :  

public static void Jsonstringertest () {Jsonstringer stringer = new Jsonstringer ();      

load JSON format to write a demo ( Printwriter+jsonstringer can write to JSON file ):  

public static void JsonStringerTest2 () throws FileNotFoundException {Jsonstringer Jsonstringer = new Jsonstringer ();     Jsonobject obj6 = new Jsonobject (); Obj6.put ("title", "Book1"). Put ("Price", "$11");     Jsonobject obj3 = new Jsonobject (); Obj3.put ("book", Obj6);  Obj3.put ("Author", New Jsonobject (). Put ("name", "Author-1"));     Jsonobject obj5 = new Jsonobject (); Obj5.put ("title", "Book2"). Put ("Price", "$22");     Jsonobject obj4 = new Jsonobject ();  Obj4.put ("book", Obj5);  Obj4.put ("Author", New Jsonobject (). Put ("name", "Author-2"));     Jsonarray obj2 = new Jsonarray ();  Obj2.put (OBJ3). put (OBJ4);     Jsonobject obj1 = new Jsonobject ();     Obj1.put ("title", "book");      Obj1.put ("Signing", OBJ2);     Jsonstringer.object (). Key ("session"). Value (obj1). EndObject ();  System.out.println (Jsonstringer.tostring ()); PrintWriter out = new PrintWriter (New FileOutputStream ("1.txt"));     Out.println (Jsonstringer.tostring ()); Out.close ();  } 

4.jsontokener

    • It is used with Jsonobject and Jsonarray constructors to parse the JSON source string  

The code is shown below (Jsonobject+jsontokener can get JSON-formatted text objects ):  

public static void Jsontokenertest () throws FileNotFoundException {     Jsonobject jsonobj = new Jsonobject (New Jsontoken ER (new FileReader (New File ("1.txt")));     

Note: In Java, a JSON-formatted string is best represented by single quotation marks  

JSON parsing tool-org.json use tutorial

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.