Parsing method of JSON under Java-----Gson and Jsonobject

Source: Internet
Author: User
Tags string to json tojson
Gson:

Gson is a JSON format parsing package developed by Google, which is characterized by the need to know the JSON data format that is transmitted before parsing JSON, and to define a series of classes that have the same hierarchical structure as the JSON hierarchy. In other words, if the transmitted JSON structure is:

{
    ' name ': ' Relin ',
    ' sex ': ' Male ',
    ' age ':
}
Then, you must define a class whose name is exactly the same as the name of the attribute in JSON:

Class Person {public
    String name;
    public String sex;
    public int age;
}

Gson parsing JSON has three features: if a predefined class does not contain a property in JSON, the property will not be parsed, but the other members will still be able to parse the name exactly the same, otherwise the member variable of the normal parsing class may be public or To be private

Let's take a look at two simple parsing and reverse parsing processes:

1. Define class:

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 ' name to set     *   
	/public void SetName (String name) {  
		this.name = name;  
	}    
	
	/**     * @return The Age    
	/public int getage () {return age        
		;    
	}    
	
	/** * @param age "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 < 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 < 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

Another Java API that uses and parses JSON is from Org.json. With its relatively intuitive, it contains a major class: Jsonobject is fairly jsonarray with the dictionary type in JSON and the array type in JSON

The basic usage is as follows:

       Converts a string string to a JSON object
jsonobject jsonobject = new Jsonobject (strbody);
            Extracts attributes of type int whose property name is from and other Boolean types are similar to
int from = Jsonobject.getint ("from");
            Extracts the JSON array
jsonarray todata= jsonobject.getjsonarray ("Todata");



Related Article

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.