- 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)  
 
-     {  
 
-         
 
-         
 
-         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);  
 
-   
 
-         
 
-         String jsonStr = "{\"Name\":\"\u5e0c\u4e9a\",\"Age\":20}";  
 
-         Object obj = JSONValue.parse(jsonStr);  
 
-         System.out.println(obj);  
 
-         System.out.println();  
 
-   
 
-         
 
-         
 
-         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);  
 
-   
 
-         
 
-         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);  
 
-         }  
 
-         
 
-         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());  
 
-   
 
-             
 
-             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)  
 
-     {  
 
-         
 
-         
 
-         JSONObject obj = new JSONObject();  
 
-         obj.put(key, value);    
 
-         return obj.toString();  
 
-     }  
 
-       
 
-     
 
-     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":"Japan","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  
 
 JSON字符串转换JAVA对象例子。
原文:http://www.cnblogs.com/zhengteng/p/5266574.html