JsonTest:
package com.demo.json; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.json.simple.JSONValue; public class JsonTest { public static void main(String[] args) { // ----------------------------------------------------------------------------- // Object 2 JSON List<Peoper> peopers = new ArrayList<Peoper>(); Peoper p1 = new Peoper("001", "Taki", "中国"); Peoper p2 = new Peoper("002", "DSM", "China"); peopers.add(p1); peopers.add(p2); String result = JsonTool.getJsonString("Peopers", peopers); System.out.println("JSON: " + result); // 解析PHP json_encode 字符串 String jsonStr = "{\"Name\":\"\u5e0c\u4e9a\",\"Age\":20}"; Object obj = JSONValue.parse(jsonStr); System.out.println(obj); System.out.println(); // ----------------------------------------------------------------------------- // JSON 2 Object String jsonString = "[" + "{\"author\":\"7\",\"id\":358,\"title\":\"Japan\",\"pictures\":[{\"description\":\"001\",\"imgPath\":\"/cms/u/cms/www/201203/05150720ii68.jpg\"},{\"description\":\"002\",\"imgPath\":\"/cms/u/cms/www/201203/05150720ii67.jpg\"}],\"path\":\"ip\"}," + "{\"author\":\"8\",\"id\":359,\"title\":\"China\",\"pictures\":[{\"description\":\"101\",\"imgPath\":\"/cms/u/cms/www/201203/111111111111.jpg\"},{\"description\":\"102\",\"imgPath\":\"/cms/u/cms/www/201203/222222222222.jpg\"}],\"path\":\"ip\"}]"; JSONArray array = JSONArray.fromObject(jsonString); // Content.class包含pictures.class,需要设置这个参数 Map<String, Class<pictures>> classMap = new HashMap<String, Class<pictures>>(); classMap.put("pictures", pictures.class); Object[] objs = new Object[array.size()]; for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); objs[i] = (Content) JSONObject.toBean(jsonObject, Content.class, classMap); } // 转换Content,循环输出所有content for (int i = 0; i < objs.length; i++) { Content content = (Content) objs[i]; System.out.println("author:" + content.getAuthor() + " ID:" + content.getId() + " Title:" + content.getTitle() + " Path:" + content.getPath()); // 转换pictures,循环输出所有picture List<pictures> pictures = content.getPictures(); for (int n = 0; n < pictures.size(); n++) System.out.println("description:" + pictures.get(n).getDescription() + " imgPath:" + pictures.get(n).getImgPath()); } } }
JsonTool:
package com.demo.json; import java.util.regex.Matcher; import java.util.regex.Pattern; import net.sf.json.JSONObject; public class JsonTool { public static String getJsonString(Object key, Object value) { //System.out.println("key: " + key); //System.out.println("value: " + value.toString()); JSONObject obj = new JSONObject(); obj.put(key, value); //添加物件 return obj.toString(); //转换为字符串并返回 } //解析PHP json_encode 字符串 public static String unescapeUnicode(String str) { StringBuffer b=new StringBuffer(); Matcher m = Pattern.compile("\\\\u([0-9a-fA-F]{4})").matcher(str); while(m.find()) { b.append((char)Integer.parseInt(m.group(1),16)); } return b.toString(); } }
People:
package com.demo.json; public class People { public People() { } public People(String id, String name, String address) { this.id = id; this.name = name; this.address = address; } private String id; private String name; private String address; public String getId() { return id; } public void setId(String 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; } public String toString() { return "ID:" + this.id + " Name:" + this.name + " Address:" + this.address; } }
Content:
package com.demo.json; import java.util.List; public class Content { private String author; private String id; private String title; private List<pictures> pictures; private String path; public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<pictures> getPictures() { return pictures; } public void setPictures(List<pictures> pictures) { this.pictures = pictures; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } }
pictures:
package com.demo.json; public class pictures { private String description; private String imgPath; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getImgPath() { return imgPath; } public void setImgPath(String imgPath) { this.imgPath = imgPath; } }
控制台运行结果:
JSON: {"Peopers":[{"address":"中国","id":"001","name":"Taki"},{"address":"China","id":"002","name":"DSM"}]}
{"Name":"中文内容","Age":20}
author:7 ID:358 Title:Japan Path:ip
description:001 imgPath:/cms/u/cms/www/201203/05150720ii68.jpg
description:002 imgPath:/cms/u/cms/www/201203/05150720ii67.jpg
author:8 ID:359 Title:China Path:ip
description:101 imgPath:/cms/u/cms/www/201203/111111111111.jpg
description:102 imgPath:/cms/u/cms/www/201203/222222222222.jpg