zoukankan      html  css  js  c++  java
  • [转]json+JSONObject+JSONArray 结合使用

    JSONObject与JSONArray的区别简述:

    区别在于JSONObject是一个{}包裹起来的一个对象(Object),
    而JSONArray则是[]包裹起来的一个数组(Array),
    说白点就是一个是数组一个是对象或字符串。



    例1:

    package com.rtt.lltest;

    import java.util.HashMap;
    import java.util.Map;

    import org.json.JSONArray;

    import com.rtt.platform.system.util.JSONUtil;

    import net.sf.json.JSONObject;

    public class Test {
     
      public static void main(String[] args) {
      // Object 类型字符串
      String json = "{"name":"reiz","age":"32"}";
      JSONObject jsonObj = JSONObject.fromObject(json);
      String name = jsonObj.getString("name");
      System.out.println(name+"||||||||||||||");
      // 结果:reiz
      jsonObj.put("initial", name.substring(0, 1).toUpperCase());
      // jsonObject 添加数组
      String[] likes = new String[] { "JavaScript", "Skiing", "Apple Pie" };
      jsonObj.put("likes", likes);
      // jsonObject 添加Map
      Map<String, String> ingredients = new HashMap<String, String>();
      ingredients.put("apples", "3kg");
      ingredients.put("sugar", "1kg");
      ingredients.put("pastry", "2.4kg");
      ingredients.put("bestEaten", "outdoors");
      jsonObj.put("ingredients",ingredients);
     
      System.out.println(jsonObj);
      System.out.println(jsonObj.getString("likes"));
      System.out.println(jsonObj.getString("ingredients"));
     
      org.json.JSONObject jsonObje = new org.json.JSONObject();
      JSONUtil.put(jsonObje, "perNum", "lisi");
      JSONUtil.put(jsonObje, "cardNum", "12345600");
      org.json.JSONObject jsonObjee = new org.json.JSONObject();
      JSONUtil.put(jsonObjee, "perNum", "lilei");
      JSONUtil.put(jsonObjee, "cardNum", "123456");
      JSONArray jsonArray = new JSONArray();
      jsonArray.put(jsonObje);
      jsonArray.put(jsonObjee);
      System.out.println("---------------------------------");
      System.out.println(jsonArray.toString());
     
      }
    }

    =========================================================

    JSONUtil.java

    public static void put(JSONObject jsonObj, String key, Object value) {

    if (value == null) {

    jsonObj.put(key, StringPool.BLANK);

    }

    else {

    jsonObj.put(key, value.toString());

    }

    }

     

    例2:JSONArray和JSONObject互相添加

    package com.rtt.lltest;

    import net.sf.json.JSONArray;

    import net.sf.json.JSONObject;

    public class ObjTest {

     
     public static void main(String[] args) {
      // TODO Auto-generated method stub
      
      JSONObject jo = new JSONObject();
      jo.put("isleaf", true);
      jo.put("name", "zhangsan");
      jo.put("age", "25");
      
      JSONObject jo2 = new JSONObject();
      jo2.put("isleaf", false);
      jo2.put("name", "lisi");
      jo2.put("age", "25");
      
      JSONObject jo3 = new JSONObject();
      jo3.put("isleaf", true);
      jo3.put("name", "lisi");
      jo3.put("age", "25");
      
      JSONArray ja0 = new JSONArray();
      //把JSONObject添加到中JSONArray
      ja0.add(jo3);
      //把JSONArray添加到JSONObject中
      jo2.element("children", ja0);
      System.out.println(jo2.toString());
      
      JSONArray ja1 = new JSONArray();
      ja1.add(jo);
      ja1.add(jo2);
      ja1.add(jo3);
      System.out.println("===================================+++++++++++++++++++++");
      System.out.println(ja1.toString());
      System.out.println("===================================+++++++++++++++++++++");
      
      
     }

    }

     

  • 相关阅读:
    js dom
    js Number string
    jq ajax数据交互
    js date 和 math
    js中英文网页切换
    日常使用
    php求和
    empty()
    时间戳、日期相互转换
    数组转字符串之间相互转换
  • 原文地址:https://www.cnblogs.com/ZhuRenWang/p/4765524.html
Copyright © 2011-2022 走看看