Java Generate JSON Data sample _java

Source: Internet
Author: User



Jsontools.java

Copy Code code as follows:

Package com.lihua.json.tools;

Import Net.sf.json.JSONObject;

public class Jsontools {

Public Jsontools () {

}

/**
* @param key
* Represents the header information for a JSON string
* @param value
* is the type of the Resolved collection
* @return
*/
Convert data to JSON
public static string Createjsonstring (string key, Object value) {
Jsonobject jsonobject = new Jsonobject ();
Jsonobject.put (key, value);
return jsonobject.tostring ();

}

}

Person.java:

Copy Code code as follows:

Package com.lihua.json.domain;

public class Person {

private int id;
private String name;
Private String address;


Public person () {
}

public person (int ID, string name, string address) {
Super ();
This.id = ID;
THIS.name = name;
this.address = address;
}

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 getaddress () {
return address;
}

public void setaddress (String address) {
this.address = address;
}

Overriding the ToString () method
@Override
Public String toString () {
Return ' person [id= + ID + ', name= ' + name + ', address= ' + address
+ "]";
}

}

Jsonservice.java:

Copy Code code as follows:

Package com.lihua.json.service;

Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;

Import Com.lihua.json.domain.Person;

public class Jsonservice {

Public Jsonservice () {

}

Declare a method of a person type and add a single object to it
Public Person Getperson () {
person person = new Person (1001, "Jack", "Beijing");
return person;

}
Declare a method of a list<person> type and add multiple objects to it
Public list<person> Getlistperson () {
list<person> list = new arraylist<person> ();
Person Person1 = new Person ("Jack", "Guangdong");
Person Person2 = new Person ("Rose", "Guangxi");
List.add (Person1);
List.add (Person2);
return list;
}
Declares a method of a list<string> type and adds a single object to it
Public list<string> getliststring () {
list<string> list = new arraylist<string> ();
List.add ("Beijing");
List.add ("Shanghai");
List.add ("Guangdong");
List.add ("Guangxi");
return list;
}

Declare a method of a list<map<string,object>> type and add multiple objects to it
Public list<map<string,object>> Getlistmap () {

list<map<string,object>> list = new arraylist<map<string,object>> ();
map<string,object> map1 = new hashmap<string,object> ();
Map1.put ("id", 100);
Map1.put ("name", "Jack");
Map1.put ("Address", "Beijing");

map<string,object> map2 = new hashmap<string,object> ();
Map2.put ("id", 100);
Map2.put ("name", "Rose");
Map2.put ("Address", "Shanghai");

List.add (MAP1);
List.add (MAP2);
return list;
}


}

Test.java:

Copy Code code as follows:

Package com.lihua.json.test;

Import java.util.List;
Import Java.util.Map;

Import Com.lihua.json.domain.Person;
Import Com.lihua.json.service.JsonService;
Import Com.lihua.json.tools.JsonTools;

public class Test {

Public Test () {

}

public static void Main (string[] args) {
String msg = "";
New A Jsonservice object
Jsonservice service = new Jsonservice ();
Declares a person object and obtains data from the person object
Person person = Service.getperson ();
Converts the data in the person object to JSON data and saves it to MSG
msg = jsontools.createjsonstring ("person", person);
SYSTEM.OUT.PRINTLN ("---->" +msg);

list<person> list = Service.getlistperson ();
msg = jsontools.createjsonstring ("person", list);
SYSTEM.OUT.PRINTLN ("---->" +msg);


list<map<string, object>> listmap = Service.getlistmap ();
msg = jsontools.createjsonstring ("person", listmap);
SYSTEM.OUT.PRINTLN ("---->" +msg);
}
}

Run Result:

PS: About JSON operation, here we recommend a few more practical JSON online tools for your reference to use:

Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json

JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat

Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson

JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat

Online JSON compression/escape tool:

Http://tools.jb51.net/code/json_yasuo_trans

C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json

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.