json conversion in java

JSON online translation tool:

http://www.bejson.com/jsoneditoronline/

 

The first:

First download the required function library, I am using google-gson-2.2.4 here (download link: http://download.csdn.net/detail/a771948524/6668573 ). After the download is complete, create a new lib file on the project, copy the downloaded file into it, right-click and select Add to Build Path. So far, the preparation work is basically completed.

The next step is to write code. Here, I paste the code as follows:

[java] view plain copy
 
print ?
  1. import com.google.gson.JsonArray;  
  2. import  com.google.gson.JsonObject;  
  3.   
  4. /** 
  5.  * @date 2015-05-25 
  6.  * @author jack  
  7.  * 
  8.  */  
  9. publicclass CreateJson {   
  10.   
  11.     publicstaticvoid main(String[] args) {    
  12.         //Create a new json object and add properties directly  
  13.         JsonObject jsonObject = new JsonObject();  
  14.         jsonObject.addProperty("cat""it");  
  15.           
  16.         //Create a json array, which is the data of one or more groups of json objects  
  17.         JsonArray jsonArray = new JsonArray();  
  18.   
  19.         JsonObject jObject1 = new JsonObject();  
  20.         jObject1.addProperty("id"1);  
  21.         jObject1.addProperty("name""java");  
  22.         jObject1.addProperty("ide""Eclipse");  
  23.         jsonArray.add(jObject1);  
  24.   
  25.         JsonObject jObject2 = new JsonObject();  
  26.         jObject2.addProperty("id"2);  
  27.         jObject2.addProperty("name""Swift");  
  28.         jObject2.addProperty("ide""X-code");  
  29.         jsonArray.add(jObject2);  
  30.   
  31.         JsonObject jObject3 = new JsonObject();  
  32.         jObject3.addProperty("id"3);  
  33.         jObject3.addProperty("name""C#");  
  34.         jObject3.addProperty("ide""Visual Studio");  
  35.         jsonArray.add(jObject3);  
  36.   
  37.         jsonObject.add("languages", jsonArray);  
  38.         jsonObject.addProperty("pop""true");  
  39.         System.out.println(jsonObject.toString());  
  40.     }  
  41. }  
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**
 * @date 2015-05-25
 * @author jack
 *
 */
public class CreateJson {

	public static void main(String[] args) {
		//Create a new json object and add properties directly
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("cat", "it");
		
		//Create a json array, which is the data of one or more groups of json objects
		JsonArray jsonArray = new JsonArray();

		JsonObject jObject1 = new JsonObject();
		jObject1.addProperty("id", 1);
		jObject1.addProperty("name", "java");
		jObject1.addProperty("ide", "Eclipse");
		jsonArray.add(jObject1);

		JsonObject jObject2 = new JsonObject();
		jObject2.addProperty("id", 2);
		jObject2.addProperty("name", "Swift");
		jObject2.addProperty("ide", "X-code");
		jsonArray.add(jObject2);

		JsonObject jObject3 = new JsonObject();
		jObject3.addProperty("id", 3);
		jObject3.addProperty("name", "C#");
		jObject3.addProperty("ide", "Visual Studio");
		jsonArray.add(jObject3);

		jsonObject.add("languages", jsonArray);
		jsonObject.addProperty("pop", "true");
		System.out.println(jsonObject.toString());
	}
}

The second:

 

java json string to JSONObject and JSONArray and value
  1. import net.sf.json.JSONArray;  
  2. import net.sf.json.JSONObject;  
  3.   
  4. public class JsonTest {  
  5.     public static void main(String[] args) {  
  6.         String joStr = "{name:\"张三\",age:\"20\"}";  
  7.         //Convert json string to JSONObject  
  8.         JSONObject jsonObject = JSONObject.fromObject(joStr);  
  9.         //Get the information inside through getString("") respectively  
  10.         String name = jsonObject.getString("name");  
  11.         String age = jsonObject.getString("age");  
  12.         //output Zhang San 20  
  13.         System.out.println(name+"  "+age);  
  14.           
  15.         String jaStr = "[{user:{name:\"张三\",age:\"20\"}},{score:{yuwen:\"80\",shuxue:\"90\"}}]";  
  16.         //Convert jsonArray string to JSONArray  
  17.         JSONArray jsonArray = JSONArray.fromObject(jaStr);  
  18.         // get the first element of the array  
  19.         JSONObject jUser = jsonArray.getJSONObject(0).getJSONObject("user");  
  20.         //Take out the information of the first element and convert it to JSONObject  
  21.         String name2 = jUser.getString("name");  
  22.         String age2 = jUser.getString("age");  
  23.         //output Zhang San 20  
  24.         System.out.println(name2+"  "+age2);  
  25.         //Take out the second element of the array and convert it to JSONObject  
  26.         JSONObject jScore = jsonArray.getJSONObject(1).getJSONObject("score");  
  27.         //Retrieve the information of the second element  
  28.         String yuwen = jScore.getString("yuwen");  
  29.         String shuxue = jScore.getString("shuxue");  
  30.         // output 80 90  
  31.         System.out.println(yuwen+"   "+shuxue);  
  32.     }  
  33.       
  34. }  

The third:

 

Jackson is a class library that Java uses to process data in JSON format with very good performance.

 

Example:

 

{
   : {  : ,  :  },
   : ,
   : ,
   : 
}

 

 class User {
      Gender { MALE, FEMALE };

      class Name {
        _first, _last;

        getFirst() {  _first; }
        getLast() {  _last; }

       void setFirst( s) { _first = s; }
       void setLast( s) { _last = s; }
    }

     Gender _gender;
     Name _name;
      _isVerified;
     [] _userImage;

     Name getName() {  _name; }
      isVerified () {_isVerified; }
     Gender getGender() {  _gender; }
     [] getUserImage () {_userImage; }}

     void setName(Name n) { _name = n; }
     void setVerified( b) { _isVerified = b; }
     void setGender(Gender g) { _gender = g; }
     void setUserImage([] b) { _userImage = b; }
}

 

ObjectMapper mapper =  ObjectMapper(); User user = mapper.readValue( File(), User.class);

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326568491&siteId=291194637