JsonTools.java
The code copy is as follows:
package com.lihua.json.tools;
import net.sf.json.JSONObject;
public class JsonTools {
public JsonTools() {
}
/**
* @param key
* Header information representing json string
* @param value
* is the type of the parsed 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:
The code copy is 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;
}
//Rewrite the toString() method
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", address=" + address
+ "]";
}
}
JsonService.java:
The code copy is 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 Person-type method and add a single object to it
public Person getPerson() {
Person person = new Person(1001,"jack","beijing");
return person;
}
//Declare a List<Person> method and add multiple objects to it
public List<Person> getlistPerson() {
List<Person> list = new ArrayList<Person>();
Person person1 = new Person(100,"jack","Guangdong");
Person person2 = new Person(101,"rose","Guangxi");
list.add(person1);
list.add(person2);
return list;
}
//Declare a method of type List<String> and add 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 type List<Map<String,Object>> 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:
The code copy is 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();
//Declare a Person object and obtain the data in the person object
Person person = service.getPerson();
//Convert the data in the person object to json data and save 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);
}
}
Running results:
PS: Regarding json operation, here are some practical json online tools for your reference:
Online JSON code verification, inspection, beautification and formatting tools:
http://tools.VeVB.COM/code/json
JSON online formatting tool:
http://tools.VeVB.COM/code/jsonformat
Online XML/JSON mutual conversion tool:
http://tools.VeVB.COM/code/xmljson
json code online formatting/beautification/compression/editing/converting tools:
http://tools.VeVB.COM/code/jsoncodeformat
Online json compression/escaping tools:
http://tools.VeVB.COM/code/json_yasuo_trans
C language style/HTML/CSS/json code formatting and beautification tools:
http://tools.VeVB.COM/code/ccode_html_css_json