GSON:
Gson is a JSON format parsing package developed by google. It is characterized by knowing the JSON data format transmitted before parsing json, and defining a series of classes with the same hierarchical structure as json. In other words, if the JSON structure of the transmission is:
{ "name":"relin", "sex":"male", "age":26 }
Then, you must predefine a class whose member variable name is exactly the same as the attribute name in json:
class Person { public String name; public String sex; public int age; }
Gson parsing json has three characteristics:
- If a predefined class does not contain an attribute in json, the attribute will not be parsed, but other members will still be able to parse normally
- Names must be identical, otherwise they will not be parsed properly
- The member variables of a class can be public or private
Let's look at two simple analytical and anti-analytical processes:
1. Define classes:
package com.relin.gson.data; public class Person { private String name; private int age; private int sex; /** * @return the name */ public String getName() { return name+"*****"; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } @Override public String toString() { return name + ":" + age; } }
2. String to json:
private static boolean StringToJson(){ try{ String str = "{\"name\":\"name0\",\"age\":0}"; Gson gson = new Gson(); Person person= gson.fromJson(str, Person.class); System.out.println(person); } catch (Exception e){ return false; } return true; }
3. Json to String:
private static boolean JsonToString(){ try{ Gson gson = new Gson(); ArrayList<Person> persons = new ArrayList<Person>(); for (int i = 0; i < 10; i++) { Person p = new Person(); p.setName("name" + i); p.setAge(i * 5); persons.add(p); } String str = gson.toJson(persons); System.out.println(str); } catch (Exception e){ return false; } return true; }
4. The call can be as follows:
package com.relin.gson; import java.util.ArrayList; import com.google.gson.Gson; import com.relin.gson.data.Person; import com.relin.gson.data.UrlResponse; public class Example { private static boolean JsonToString(){ try{ Gson gson = new Gson(); ArrayList<Person> persons = new ArrayList<Person>(); for (int i = 0; i < 10; i++) { Person p = new Person(); p.setName("name" + i); p.setAge(i * 5); persons.add(p); } String str = gson.toJson(persons); System.out.println(str); } catch (Exception e){ return false; } return true; } private static boolean StringToJson(){ try{ String str = "{\"name\":\"name0\",\"age\":0}"; Gson gson = new Gson(); Person person= gson.fromJson(str, Person.class); System.out.println(person); } catch (Exception e){ return false; } return true; } public static void main(String agrs[]){ StringToJson(); JsonToString() } }
JSONObject is a final class which inherits Object and implements JSON interface.
The construction method is as follows:
JSONObject(); Create an empty JSONObject object
JSONObject(boolean isNull); Create a JSONObject object that is empty or not
The general methods are as follows:
fromBean(Object bean); Static method to create a JSONObject object through a pojo object
fromJSONObject(JSONObject object); static method to construct a JSONObject object from another JSONObject object
fromJSONString(JSONString string); static method to create a JSONObject object through a JSONString
toString(); Converts a JSONObject object to a string in json format
iterator(); returns an Iterator object to traverse elements
It contains one main class:
1.JSONObject is equivalent to the dictionary type in json
2.JSONArray is equivalent to the array type in json
The basic usage is as follows:
import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JSONObjectSample { //Create JSONObject objects private static JSONObject createJSONObject(){ JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "kevin"); jsonObject.put("Max.score", new Integer(100)); jsonObject.put("Min.score", new Integer(50)); jsonObject.put("nickname", "picglet"); return jsonObject; } public static void main(String[] args) { JSONObject jsonObject = JSONObjectSample.createJSONObject(); //Output jsonobject object object System.out.println("jsonObject==>"+jsonObject); //Interpret the type of output object boolean isArray = jsonObject.isArray(); boolean isEmpty = jsonObject.isEmpty(); boolean isNullObject = jsonObject.isNullObject(); System.out.println("isArray:"+isArray+" isEmpty:"+isEmpty+" isNullObject:"+isNullObject); //Adding attributes jsonObject.element("address", "swap lake"); System.out.println("Objects after adding attributes==>"+jsonObject); //Return a JSONArray object JSONArray jsonArray = new JSONArray(); jsonArray.add(0, "this is a jsonArray value"); jsonArray.add(1,"another jsonArray value"); jsonObject.element("jsonArray", jsonArray); JSONArray array = jsonObject.getJSONArray("jsonArray"); System.out.println("Return a JSONArray Objects:"+array); //Value after adding JSONArray //{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake", //"jsonArray":["this is a jsonArray value","another jsonArray value"]} System.out.println(jsonObject); //Returns a string based on key String jsonString = jsonObject.getString("name"); System.out.println("jsonString==>"+jsonString); //Parsing a json object (which can parse different types of data) jsonObject = getJSONObject("{d:test,e:true,b:1.1,c:1,a:1}"); System.out.println(jsonObject); //{"d":"test","e":true,"b":1.1,"c":1,"a":1} System.out.println(jsonObject.getInt("a")); System.out.println(jsonObject.getDouble("b")); System.out.println(jsonObject.getLong("c")); System.out.println(jsonObject.getString("d")); System.out.println(jsonObject.getBoolean("e")); } public static JSONObject getJSONObject(String str) { if (str == null || str.trim().length() == 0) { return null; } JSONObject jsonObject = null; try { jsonObject = new JSONObject(str); } catch (JSONException e) { e.printStackTrace(System.err); } return jsonObject; } }