首页 > 代码库 > 通过java代码来完成对于json格式的转换
通过java代码来完成对于json格式的转换
/** * */package net.nyist.jsondemo.servlet;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.json.JSONObject;/** * @author yuchao * * @school 南阳理工软件学院移动设备应用与开发11软工移动四班 * * @time 2014-9-17 下午9:24:29 */public class JSONDemo { public static void main(String[] args) { Map<String, Object> map =new HashMap<String, Object>(); List<Map<String, Object>> list =new ArrayList<Map<String, Object>>(); map.put("age", 12); map.put("name", "yuchao"); list.add(map); map =new HashMap<String, Object>(); map.put("age", 21); map.put("name", "zhangsan"); list.add(map); map =new HashMap<String, Object>(); map.put("users", list); JSONObject jsonObject =new JSONObject(map); System.out.println(jsonObject.toString()); }}
/** * */package net.nyist.jsondemo.entity;import java.io.Serializable;/** * @author yuchao * * @school 南阳理工软件学院移动设备应用与开发11软工移动四班 * * @time 2014-9-17 下午11:53:32 */@SuppressWarnings("serial")public class Address implements Serializable{ private String codeNumber; private String city; public String getCodeNumber() { return codeNumber; } public void setCodeNumber(String codeNumber) { this.codeNumber = codeNumber; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }/** * */package net.nyist.jsondemo.entity;import java.io.Serializable;/** * @author yuchao * * @school 南阳理工软件学院移动设备应用与开发11软工移动四班 * * @time 2014-9-17 下午11:54:43 */@SuppressWarnings("serial")public class User implements Serializable{ private int id; private String name; private int age; private 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; }}/** * */package net.nyist.jsondemo.servlet;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import net.nyist.jsondemo.entity.Address;import net.nyist.jsondemo.entity.User;import org.json.JSONArray;/** * @author yuchao * * @school 南阳理工软件学院移动设备应用与开发11软工移动四班 * * @time 2014-9-17 下午11:53:03 */public class JSONObjectDemo { public static void main(String[] args) { Address address =new Address(); address.setCity("河南南阳"); address.setCodeNumber("473000"); User user =new User(); user.setId(1); user.setName("yuchao"); user.setAddress(address); Map<String , Object> map =new HashMap<String, Object>(); List<User> list =new ArrayList<User>(); list.add(user); address =new Address(); address.setCity("河南郑州"); address.setCodeNumber("473000"); user =new User(); user.setId(2); user.setName("liling"); user.setAddress(address); list.add(user); map.put("users", list); JSONArray jsonArray =new JSONArray(); jsonArray.put(map); System.out.println(jsonArray.toString(3)); } }
通过java代码来完成对于json格式的转换
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。