Parsing JSON data series 1: Displaying JSON data on a Web page __json

Source: Internet
Author: User
Tags stub

JSON's full name: Javascriptobjectnotation


The two constructs of JSON: a collection of name/value pairs, an ordered list of values.


Mobile Clients (Android and iphone) have no independent platform for data and platforms to be returned, which can be Java,. NET, or PHP. The
Mobile client requests the server side, typically using the lightweight form of JSON.


JSON data format:
The JSON object is an unordered set of ' name/value pairs '.
An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends.
Each name is followed by a ":" (colon), and "' Name/value ' pair", separated by the "," (comma).
such as: {
        "name": "Jackson"
        "age": +
   
An array is an ordered collection of values (value). An array begins with "[" (opening parenthesis), and "]" (right bracket) ends. Use "," (comma) between values to separate
such as: {
        "students":
        [
    &NB Sp       {"Name": "Jackson", "Age": +},
            {"name": "Michael", "Age" : I
       ]
   


This example uses the server side: (Jsp+servlet) (the string that generates the JSON), Client: Parses JSON data. A string that displays JSON on a Web page.

The whole idea: Create a new Web project, define an entity class person, define three fields inside, define its constructor, get, set method, ToString method. Define a Jsonservice class in which you define the Getperson method to return a person object, and define the Getlistperson method to return a list object containing the person type. Define the Getliststring method returns a list object with a string type, defining the Getlistmaps method to return a list object with a map type. Define a Jsontools class in which you write a method to build a JSON string. Defines a Testjson class in which the main method is defined, in which the call Createjsonstring returns the JSON object and outputs it. Defines a servlet class jsonaction, in which the code in the Doget method is changed to This.dopost (request, response), the way in which the request and reception are added to the DoPost method, Call Createjsonstring returns the JSON object and outputs it.

Person.java file:

public class Person {

	private int id;
	private String name;
	Private String adress;

	public person (int ID, string name, string adress) {
		super ();
		This.id = ID;
		this.name = name;
		this.adress = adress;
	}

	Public person () {
		//TODO auto-generated constructor stub
	} public

	int getId () {return
		ID;
	} Public

	void setId (int id) {
		this.id = ID;
	}

	Public String GetName () {return
		name;
	}

	public void SetName (String name) {
		this.name = name;
	}

	Public String getadress () {return
		adress;
	}

	public void setadress (String adress) {
		this.adress = adress;
	}

	@Override public
	String toString () {return
		] person [id= + ID +, name= "+ name +", adress= "+ adress
				+ "]";
	}

}

Jsonservice.java file:

public class Jsonservice {public Jsonservice () {//TODO auto-generated constructor stub} public person Getperso
		N () {Person of person =new person (1001, "Jack", "Beijing");
		
	return person;
		Public list<person> Getlistperson () {list<person> list=new arraylist<person> ();
		Person Person1=new person (1001, "Jack", "Beijing");
		Person Person2=new person (1001, "Rose", "Shanghai");
		List.add (Person1);
		
		List.add (Person2);
	return list;
		Public list<string> getliststring () {list<string> list=new arraylist<string> ();
		List.add ("Beijing");
		List.add ("Shanghai");
		List.add ("Guangzhou");
	return list; Public list<map<string, Object>> getlistmaps () {list<map<string, object>> list=new ArrayList
		<Map<String,Object>> ();
		Map<string, object> map1=new hashmap<string, object> ();
		Map1.put ("id", 1001);
		Map1.put ("name", "Jack");
		Map1.put ("Address", "Beijing"); Map<string, object> Map2=new hasHmap<string, object> ();
		Map2.put ("id", 1002);
		Map2.put ("name", "Rose");
		Map2.put ("Address", "Shanghai");
		List.add (MAP1);
		List.add (MAP2);
	return list;
 }
}

Jsontools.java file:

public class Jsontools {public

	jsontools () {
		//TODO auto-generated constructor stub
	}
	/**
	 * 
	 * @param key  represents the header information for the JSON string
	 * @param value is the type of the  resolved collection
	 * @return/public
	static String Createjsonstring (String key,object value) {
		 jsonobject jsonobject=new jsonobject ();
		 Jsonobject.put (key, value);
		 return jsonobject.tostring ();
		 
	}

Testjson.java file:
public class Testjson {public

	Testjson () {
		//TODO auto-generated constructor stub
	} public
    
	static V OID Main (string[] args) {
		String msg= "";
		Jsonservice service=new Jsonservice ();
Person		Person=service.getperson ();		msg=jsontools.createjsonstring ("person", person);		System.out.println (msg);
		Msg=jsontools.createjsonstring ("Persons", Service.getlistmaps ());
		SYSTEM.OUT.PRINTLN (msg);
	}

Jsonaction.java file:

public void doget (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {

		This.dopost (request, response);

	public void DoPost (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {

		Response.setcontenttype ("Text/html;charset=utf-8");
		Request.setcharacterencoding ("Utf-8");
		Response.setcharacterencoding ("Utf-8");
		
		PrintWriter out = Response.getwriter ();
		String jsonstring=jsontools.createjsonstring ("Person", Service.getlistperson ());
		Out.println (jsonstring);
		Out.flush ();
		Out.close ();
	}







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.