加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JSONObject与JSONArray的使用

发布时间:2020-12-16 19:42:31 所属栏目:百科 来源:网络整理
导读:出处:http://www.jb51.cc/article/p-dzdxytlc-g.html Java不像PHP解析和生产JSON总是一个比较痛苦的过程。但是使用JSONObject和JSONArray会让整个过程相对舒服一些。 需要依赖的包:commons-lang.jar commons-beanutils.jar commons-collections.jar commons

出处:http://www.52php.cn/article/p-dzdxytlc-g.html

Java不像PHP解析和生产JSON总是一个比较痛苦的过程。但是使用JSONObject和JSONArray会让整个过程相对舒服一些。

需要依赖的包:commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json-lib-2.2.2-jdk15.jar

1. 创建一个JSONObject对象:

[java] view plain copy print ?
  1. packagecom.xxx.video.resource.controller.web;
  2. importjava.util.ArrayList;
  3. importjava.util.HashMap;
  4. importnet.sf.json.JSONArray;
  5. importnet.sf.json.JSONObject;
  6. publicclassTest{
  7. staticvoidmain(String[]args){
  8. //JsonObject和JsonArray区别就是JsonObject是对象形式,JsonArray是数组形式
  9. //创建JsonObject第一种方法
  10. JSONObjectjsonObject=newJSONObject();
  11. jsonObject.put("UserName","ZHULI");
  12. jsonObject.put("age","30");
  13. jsonObject.put("workIn","ALI");
  14. System.out.println("jsonObject1:"+jsonObject);
  15. //创建JsonObject第二种方法
  16. HashMap<String,String>hashMap=newHashMap<String,String>();
  17. hashMap.put("UserName","ZHULI");
  18. hashMap.put("age","30");
  19. hashMap.put("workIn","ALI");
  20. System.out.println("jsonObject2:"+JSONObject.fromObject(hashMap));
  21. //创建一个JsonArray方法1
  22. JSONArrayjsonArray=newJSONArray();
  23. jsonArray.add(0,108); list-style:decimal-leading-zero outside; color:inherit; line-height:17.27272605895996px; margin:0px!important; padding:0px 3px 0px 10px!important"> jsonArray.add(1,0); background-color:inherit">2,108); list-style:decimal-leading-zero outside; color:inherit; line-height:17.27272605895996px; margin:0px!important; padding:0px 3px 0px 10px!important"> System.out.println("jsonArray1:"+jsonArray);
  24. //创建JsonArray方法2
  25. ArrayList<String>arrayList=newArrayList<String>();
  26. arrayList.add("ZHULI");
  27. arrayList.add("30");
  28. arrayList.add("ALI");
  29. System.out.println("jsonArray2:"+JSONArray.fromObject(arrayList));
  30. //如果JSONArray解析一个HashMap,则会将整个对象的放进一个数组的值中
  31. System.out.println("jsonArrayFROMHASHMAP:"+JSONArray.fromObject(hashMap));
  32. //组装一个复杂的JSONArray
  33. JSONObjectjsonObject2=newJSONObject();
  34. jsonObject2.put("UserName",108); list-style:decimal-leading-zero outside; line-height:17.27272605895996px; margin:0px!important; padding:0px 3px 0px 10px!important"> jsonObject2.put("age",108); list-style:decimal-leading-zero outside; color:inherit; line-height:17.27272605895996px; margin:0px!important; padding:0px 3px 0px 10px!important"> jsonObject2.put("workIn",108); list-style:decimal-leading-zero outside; line-height:17.27272605895996px; margin:0px!important; padding:0px 3px 0px 10px!important"> jsonObject2.element("Array",arrayList);
  35. System.out.println("jsonObject2:"+jsonObject2);
  36. }
  37. }

结果:

[html]
    jsonObject1:{"UserName":"ZHULI","age":"30","workIn":"ALI"}
  1. jsonObject2:{"workIn":"ALI","UserName":"ZHULI"}
  2. jsonArray1:["ZHULI","30","ALI"]
  3. jsonArray2:["ZHULI","ALI"]
  4. jsonArrayFROMHASHMAP:[{"workIn":"ALI","UserName":"ZHULI"}]
  5. jsonObject2:{"UserName":"ZHULI","workIn":"ALI","Array":["ZHULI","ALI"]}

解析JSON字符串:

    packagecom.xxx.video.resource.controller.web;
  1. importnet.sf.json.JSONArray;
  2. importnet.sf.json.JSONObject;
  3. publicclassTest{
  4. publicstaticvoidmain(String[]args){
  5. StringjsonString="{"UserName":"ZHULI","age":"30","workIn":"ALI","Array":["ZHULI","30","ALI"]}";
  6. //将Json字符串转为java对象
  7. JSONObjectobj=JSONObject.fromObject(jsonString);
  8. //获取Object中的UserName
  9. if(obj.has("UserName")){
  10. System.out.println("UserName:"+obj.getString("UserName"));
  11. }
  12. //获取ArrayObject
  13. if(obj.has("Array")){
  14. JSONArraytransitListArray=obj.getJSONArray("Array");
  15. for(inti=0;i<transitListArray.size();i++){
  16. System.out.print("Array:"+transitListArray.getString(i)+"");
  17. }

返回:

    UserName:ZHULI
  1. Array:ZHULIArray:30Array:ALI


对象遍历

    if(!obj.isNull("sku")){
  1. JSONObjectskuObject=obj.getJSONObject("sku");
  2. Iterator<String>keys=skuObject.keys();
  3. Stringkey;
  4. Objecto;
  5. while(keys.hasNext()){
  6. key=keys.next();
  7. o=skuObject.get(key);
  8. skuMap.put(key,String.valueOf(o));
  9. }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读