有機肥

          綠色

          JSON字符串轉換JAVA對象例子。

          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());
                  }
              }
          }
          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();
              }
          }
          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;
              }
          }
          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;
              }
          }

          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;
              }
          }

          posted on 2016-09-18 17:52 有機肥 閱讀(227) 評論(0)  編輯  收藏

          <2016年9月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 巴楚县| 格尔木市| 洛南县| 东莞市| 奎屯市| 阿克苏市| 延边| 宝应县| 盐池县| 方正县| 崇左市| 榆树市| 富锦市| 永靖县| 庆安县| 玛沁县| 南雄市| 称多县| 镇康县| 六盘水市| 石林| 固安县| 白水县| 庆阳市| 阿拉善盟| 耒阳市| 武强县| 康定县| 南靖县| 灵台县| 会昌县| 淳化县| 丹东市| 三门峡市| 宁安市| 高密市| 醴陵市| 海南省| 延安市| 班玛县| 定南县|