zoukankan      html  css  js  c++  java
  • JSON字符串转换JAVA对象例子。

    1. package com.demo.json;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.HashMap;  
    5. import java.util.List;  
    6. import java.util.Map;  
    7.   
    8. import net.sf.json.JSONArray;  
    9. import net.sf.json.JSONObject;  
    10.   
    11. import org.json.simple.JSONValue;  
    12.   
    13. public class JsonTest  
    14. {  
    15.     public static void main(String[] args)  
    16.     {  
    17.         // -----------------------------------------------------------------------------  
    18.         // Object 2 JSON  
    19.         List<Peoper> peopers = new ArrayList<Peoper>();  
    20.         Peoper p1 = new Peoper("001", "Taki", "中国");  
    21.         Peoper p2 = new Peoper("002", "DSM", "China");  
    22.         peopers.add(p1);  
    23.         peopers.add(p2);  
    24.         String result = JsonTool.getJsonString("Peopers", peopers);  
    25.         System.out.println("JSON: " + result);  
    26.   
    27.         // 解析PHP json_encode 字符串  
    28.         String jsonStr = "{\"Name\":\"\u5e0c\u4e9a\",\"Age\":20}";  
    29.         Object obj = JSONValue.parse(jsonStr);  
    30.         System.out.println(obj);  
    31.         System.out.println();  
    32.   
    33.         // -----------------------------------------------------------------------------  
    34.         // JSON 2 Object  
    35.         String jsonString = "["  
    36.                 + "{\"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\"},"  
    37.                 + "{\"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\"}]";  
    38.         JSONArray array = JSONArray.fromObject(jsonString);  
    39.   
    40.         // Content.class包含pictures.class,需要设置这个参数  
    41.         Map<String, Class<pictures>> classMap = new HashMap<String, Class<pictures>>();  
    42.         classMap.put("pictures", pictures.class);  
    43.   
    44.         Object[] objs = new Object[array.size()];  
    45.         for (int i = 0; i < array.size(); i++)  
    46.         {  
    47.             JSONObject jsonObject = array.getJSONObject(i);  
    48.             objs[i] = (Content) JSONObject.toBean(jsonObject, Content.class, classMap);  
    49.         }  
    50.         // 转换Content,循环输出所有content  
    51.         for (int i = 0; i < objs.length; i++)  
    52.         {  
    53.             Content content = (Content) objs[i];  
    54.             System.out.println("author:" + content.getAuthor() + " ID:"  
    55.                     + content.getId() + " Title:" + content.getTitle() + " Path:" + content.getPath());  
    56.   
    57.             // 转换pictures,循环输出所有picture  
    58.             List<pictures> pictures = content.getPictures();  
    59.             for (int n = 0; n < pictures.size(); n++)  
    60.                 System.out.println("description:"  
    61.                         + pictures.get(n).getDescription() + " imgPath:" + pictures.get(n).getImgPath());  
    62.         }  
    63.     }  
    64. }  

    JsonTool:

    [java] view plain
    1. package com.demo.json;  
    2.   
    3. import java.util.regex.Matcher;  
    4. import java.util.regex.Pattern;  
    5.   
    6. import net.sf.json.JSONObject;  
    7.   
    8.   
    9. public class JsonTool   
    10. {  
    11.     public static String getJsonString(Object key, Object value)  
    12.     {  
    13.         //System.out.println("key: " + key);  
    14.         //System.out.println("value: " + value.toString());  
    15.         JSONObject obj = new JSONObject();  
    16.         obj.put(key, value);    //添加物件  
    17.         return obj.toString();  //转换为字符串并返回  
    18.     }  
    19.       
    20.     //解析PHP json_encode 字符串  
    21.     public static String unescapeUnicode(String str)  
    22.     {  
    23.         StringBuffer b=new StringBuffer();  
    24.         Matcher m = Pattern.compile("\\\\u([0-9a-fA-F]{4})").matcher(str);  
    25.         while(m.find())  
    26.         {  
    27.             b.append((char)Integer.parseInt(m.group(1),16));  
    28.         }  
    29.         return b.toString();  
    30.     }  
    31. }  

    People:

    [java] view plain
    1. package com.demo.json;  
    2.   
    3. public class People   
    4. {  
    5.     public People()   
    6.     {                 
    7.     }  
    8.       
    9.     public People(String id, String name, String address)   
    10.     {         
    11.         this.id = id;  
    12.         this.name = name;  
    13.         this.address = address;  
    14.     }  
    15.   
    16.     private String id;  
    17.     private String name;  
    18.     private String address;  
    19.       
    20.     public String getId() {  
    21.         return id;  
    22.     }  
    23.   
    24.     public void setId(String id) {  
    25.         this.id = id;  
    26.     }  
    27.   
    28.     public String getName() {  
    29.         return name;  
    30.     }  
    31.   
    32.     public void setName(String name) {  
    33.         this.name = name;  
    34.     }  
    35.   
    36.     public String getAddress() {  
    37.         return address;  
    38.     }  
    39.   
    40.     public void setAddress(String address) {  
    41.         this.address = address;  
    42.     }  
    43.       
    44.     public String toString()  
    45.     {  
    46.         return "ID:" + this.id + " Name:" + this.name + " Address:" + this.address;  
    47.     }  
    48. }  

    Content:

    [java] view plain
    1. package com.demo.json;  
    2.   
    3. import java.util.List;  
    4.   
    5. public class Content {  
    6.       
    7.     private String author;  
    8.     private String id;  
    9.     private String title;  
    10.     private List<pictures> pictures;  
    11.     private String path;  
    12.   
    13.     public String getAuthor() {  
    14.         return author;  
    15.     }  
    16.   
    17.     public void setAuthor(String author) {  
    18.         this.author = author;  
    19.     }  
    20.   
    21.     public String getId() {  
    22.         return id;  
    23.     }  
    24.   
    25.     public void setId(String id) {  
    26.         this.id = id;  
    27.     }  
    28.   
    29.     public String getTitle() {  
    30.         return title;  
    31.     }  
    32.   
    33.     public void setTitle(String title) {  
    34.         this.title = title;  
    35.     }  
    36.   
    37.     public List<pictures> getPictures() {  
    38.         return pictures;  
    39.     }  
    40.   
    41.     public void setPictures(List<pictures> pictures) {  
    42.         this.pictures = pictures;  
    43.     }  
    44.   
    45.     public String getPath() {  
    46.         return path;  
    47.     }  
    48.   
    49.     public void setPath(String path) {  
    50.         this.path = path;  
    51.     }  
    52. }  

    pictures:

    [java] view plain
    1. package com.demo.json;  
    2.   
    3. public class pictures {  
    4.     private String description;  
    5.     private String imgPath;  
    6.   
    7.     public String getDescription() {  
    8.         return description;  
    9.     }  
    10.   
    11.     public void setDescription(String description) {  
    12.         this.description = description;  
    13.     }  
    14.   
    15.     public String getImgPath() {  
    16.         return imgPath;  
    17.     }  
    18.   
    19.     public void setImgPath(String imgPath) {  
    20.         this.imgPath = imgPath;  
    21.     }  
    22. }  

    控制台运行结果:

    [plain] view plain
    1. JSON: {"Peopers":[{"address":"中国","id":"001","name":"Taki"},{"address":"Japan","id":"002","name":"DSM"}]}  
    2. {"Name":"中文内容","Age":20}  
    3.   
    4. author:7 ID:358 Title:Japan Path:ip  
    5. description:001 imgPath:/cms/u/cms/www/201203/05150720ii68.jpg  
    6. description:002 imgPath:/cms/u/cms/www/201203/05150720ii67.jpg  
    7. author:8 ID:359 Title:China Path:ip  
    8. description:101 imgPath:/cms/u/cms/www/201203/111111111111.jpg  
    9. description:102 imgPath:/cms/u/cms/www/201203/222222222222.jpg  
  • 相关阅读:
    安装pipenv
    ModuleNotFoundError: No module named 'pip._internal' , pip 无法下载软件 解决办法
    1.3用户列表 and 新闻列表
    1.2用户统计页面实现
    1.5发布新闻
    七牛云平台(存储图片)
    1.2头像设置功能
    1.4用户收藏展示
    1.3密码修改
    1.2首页刷新以及点击排行
  • 原文地址:https://www.cnblogs.com/zhengteng/p/5266574.html
Copyright © 2011-2022 走看看