Java json operation
I. JSON syntax
JSON: JavaScript Object Notation (JavaScript Object Notation). JSON is the syntax for storing and exchanging text information. Similar to XML, but smaller, faster, and easier to parse than XML.
JSON data is written in the format of name/value pair.
Name/value pair includes the field name (in double quotation marks), followed by a colon, followed by a value:
"firstName" : "John"
JSON array is written in square brackets:
An array can contain multiple objects:
{ "employees": [ { "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" } ]}
Ii. GSON Introduction
Gson is a Java class library provided by Google for ing between Java objects and JSON data. You can convert a JSON string into a Java object or vice versa. Download the jar package: http://download.csdn.net/detail/yang_xing_/8364265. Create a new java project in eclipse to introduce gson-2.2.4.jar. Create an employee. json file under the project root directory:
{ "department":"design", "employees": [ { "firstName":"Bill" , "lastName":"Gates" }, { "firstName":"George" , "lastName":"Bush" }, { "firstName":"Thomas" , "lastName":"Carter" } ]}
Ii. Reading json
First create a json parser, then create a JsonObject object, and then call the get method to access the property value:
package ucas.json.test;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import com.google.gson.JsonIOException;import com.google.gson.JsonObject;import com.google.gson.JsonParser;import com.google.gson.JsonSyntaxException;public class JsonTest { public static void main(String[] args) { try { JsonParser jParser = new JsonParser(); JsonObject jObject = (JsonObject) jParser .parse(new FileReader("employee.json")); System.out.println(jObject.get("department").getAsString()); System.out.println(jObject.get("employees")); } catch (JsonIOException e) { e.printStackTrace(); } catch (JsonSyntaxException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }}
Output result:
design[{"firstName":"Bill","lastName":"Gates"},{"firstName":"George","lastName":"Bush"},{"firstName":"Thomas","lastName":"Carter"}]
We can see that the "employees" attribute is an array, converted to JsonArray, and then read it cyclically:
package ucas.json.test;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import com.google.gson.JsonArray;import com.google.gson.JsonIOException;import com.google.gson.JsonObject;import com.google.gson.JsonParser;import com.google.gson.JsonSyntaxException;public class JsonTest { public static void main(String[] args) { try { JsonParser jParser = new JsonParser(); JsonObject jObject = (JsonObject) jParser .parse(new FileReader("employee.json")); System.out.println(jObject.get("department").getAsString()); JsonArray jArray=(JsonArray) jObject.get("employees"); for (int i = 0; i < jArray.size(); i++) { System.out.println("-----------"); JsonObject jObject2=(JsonObject) jArray.get(i); System.out.println("id:"+jObject2.get("id").getAsString()); System.out.println("firstName:"+jObject2.get("firstName").getAsString()); System.out.println("lastName:"+jObject2.get("lastName").getAsString()); } } catch (JsonIOException e) { e.printStackTrace(); } catch (JsonSyntaxException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }}
Output result:
design-----------id:1firstName:BilllastName:Gates-----------id:1firstName:GeorgelastName:Bush-----------id:3firstName:ThomaslastName:Carter
3. Create json
Create a json object, add a property using the addProperty method, and add an object using the add method. For example:
Import java. io. bufferedReader; import java. io. bufferedWriter; import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStreamWriter; import com. google. gson. jsonArray; import com. google. gson. jsonObject; public class JsonDemo2 {public static void main (String [] args) {// create json data JsonObject jsonObject = new JsonObject (); jsonObject. addProperty ("cat", "it"); JsonArray jArray = new JsonArray (); JsonObject lan1 = new JsonObject (); lan1.addProperty ("id", 1 ); lan1.addProperty ("name", "java"); lan1.addProperty ("ide", "eclipse"); jArray. add (lan1); JsonObject lan2 = new JsonObject (); lan2.addProperty ("id", 2); lan2.addProperty ("name", "C ++ "); lan2.addProperty ("ide", "VC6.0"); jArray. add (lan2); JsonObject lan3 = new JsonObject (); lan3.addProperty ("id", 3); lan3.addProperty ("name", "C #"); lan3.addProperty ("ide ", "Visual Studio"); jArray. add (lan3); JsonObject lan4 = new JsonObject (); lan4.addProperty ("id", 4); lan4.addProperty ("name", "Swift"); lan4.addProperty ("ide ", "Xcode"); jArray. add (lan4); jsonObject. add ("Ages", jArray); jsonObject. addProperty ("pop", true); System. out. println (jsonObject); File jsonFile = new File ("test. json "); try {jsonFile. createNewFile (); FileOutputStream fos = new FileOutputStream (jsonFile); OutputStreamWriter osq = new OutputStreamWriter (fos); BufferedWriter bfw = new BufferedWriter (osq); bfw. write (jsonObject. toString (); bfw. close (); osq. close (); fos. close ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}
In this way, a test. json file is generated under the project root directory:
{"Cat": "it", "Ages": [{"id": 1, "name": "Haha", "ide": "eclipse \ n "}, {"id": 2, "name": "C ++", "ide": "VC6.0" },{ "id": 3, "name ": "C #", "ide": "Visual Studio" },{ "id": 4, "name": "Swift", "ide": "Xcode"}], "pop": true}
Summary:
For the json introduction and syntax, refer to the json official website http://www.json.org/and w3ctutorial. There are a lot of json library operations, you can choose to learn.