java生成json数据示例
import net.sf.json.JSONObject;
public class JsonTools {
public JsonTools() {
}
/** * @param key * 表示json字符串的头信息 * @param value * 是对解析的集合的类型 * @return */ //将数据转换为Json public static String createJsonString(String key, Object value) { JSONObject jsonObject = new JSONObject(); jsonObject.put(key, value); return jsonObject.toString();
}
}
Person.java:
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; }
//重写toString()方法 @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", address=" + address + "]"; }
}
JsonService.java :
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() { } //声明一个Person类型的方法,并为其添加单个对象 public Person getPerson() { Person person = new Person(1001,"jack","beijing"); return person; } //声明一个List<Person>类型的方法,并为其添加多个对象 public List<Person> getlistPerson() { List<Person> list = new ArrayList<Person>(); Person person1 = new Person(100,"jack","广东"); Person person2 = new Person(101,"rose","广西"); list.add(person1); list.add(person2); return list; } //声明一个List<String>类型的方法,并为其添加单个对象 public List<String> getlistString() { List<String> list = new ArrayList<String>(); list.add("北京"); list.add("上海"); list.add("广东"); list.add("广西"); return list; } //声明一个List<Map<String,Object>>类型的方法,并为其添加多个对象 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", "北京"); Map<String,Object> map2 = new HashMap<String,Object>(); map2.put("id", 100); map2.put("name", "rose"); map2.put("address", "上海"); list.add(map1); list.add(map2); return list; } }
Test.java :
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一个JsonService对象 JsonService service = new JsonService(); //声明一个Person对象,并取得person对象中的数据 Person person = service.getPerson(); //将person对象中的数据转换为json数据,并保存到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); }}
运行结果:
以上是 java生成json数据示例 的全部内容, 来源链接: utcz.com/p/207111.html