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

JSONObject与JSONArray的使用

发布时间:2020-12-16 19:46:00 所属栏目:百科 来源:网络整理
导读: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

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对象:

package com.yunos.tv.video.resource.controller.web;

import java.util.ArrayList;
import java.util.HashMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class Test {

    public static void main(String[] args) {
        //JsonObject和JsonArray区别就是JsonObject是对象形式,JsonArray是数组形式
        //创建JsonObject第一种方法
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UserName","ZHULI");
        jsonObject.put("age","30");
        jsonObject.put("workIn","ALI");
        System.out.println("jsonObject1:" + jsonObject);
        
        //创建JsonObject第二种方法
        HashMap<String,String> hashMap = new HashMap<String,String>();
        hashMap.put("UserName","ZHULI");
        hashMap.put("age","30");
        hashMap.put("workIn","ALI");
        System.out.println("jsonObject2:" + JSONObject.fromObject(hashMap));
        
        //创建一个JsonArray方法1
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(0,"ZHULI");
        jsonArray.add(1,"30");
        jsonArray.add(2,"ALI");
        System.out.println("jsonArray1:" + jsonArray);
        
        //创建JsonArray方法2
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("ZHULI");
        arrayList.add("30");
        arrayList.add("ALI");
        System.out.println("jsonArray2:" + JSONArray.fromObject(arrayList));
        
        //如果JSONArray解析一个HashMap,则会将整个对象的放进一个数组的值中
        System.out.println("jsonArray FROM HASHMAP:" + JSONArray.fromObject(hashMap));
        
        //组装一个复杂的JSONArray
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("UserName","ZHULI");
        jsonObject2.put("age","30");
        jsonObject2.put("workIn","ALI");
        jsonObject2.element("Array",arrayList);
        System.out.println("jsonObject2:" + jsonObject2);
                
    }
}

结果:

jsonObject1:{"UserName":"ZHULI","age":"30","workIn":"ALI"}
jsonObject2:{"workIn":"ALI","UserName":"ZHULI"}
jsonArray1:["ZHULI","30","ALI"]
jsonArray2:["ZHULI","ALI"]
jsonArray FROM HASHMAP:[{"workIn":"ALI","UserName":"ZHULI"}]
jsonObject2:{"UserName":"ZHULI","workIn":"ALI","Array":["ZHULI","ALI"]}
 

解析JSON字符串:


package com.yunos.tv.video.resource.controller.web;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class Test {

public static void main(String[] args) {
String jsonString = "{"UserName":"ZHULI","age":"30","workIn":"ALI","Array":["ZHULI","30","ALI"]}";
//将Json字符串转为java对象
JSONObject obj = JSONObject.fromObject(jsonString);
//获取Object中的UserName
if (obj.has("UserName")) {
System.out.println("UserName:" + obj.getString("UserName"));
}
//获取ArrayObject
if (obj.has("Array")) {
JSONArray transitListArray = obj.getJSONArray("Array");
for (int i = 0; i < transitListArray.size(); i++) {
System.out.print("Array:" + transitListArray.getString(i) + " ");
}
}
}
}

返回:




UserName:ZHULI
Array:ZHULI Array:30 Array:ALI

json数组:将多个json对象放入数组中 JSONArray jsonArray= new JSONArray(); JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("UserName","ZHULI"); jsonObject1.put("age","30"); jsonObject1.put("workIn","ALI"); jsonArray.add(jsonObject1); JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("UserName","WANGMENG"); jsonObject2.put("age","29"); jsonObject2.put("workIn","ATT"); jsonArray.add(jsonObject2); System.out.println("jsonArray:" + jsonArray.toString()); 输出 jsonArray:[{"UserName":"ZHULI","workIn":"ALI"},{"UserName":"WANGMENG","age":"29","workIn":"ATT"}]

(编辑:李大同)

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

    推荐文章
      热点阅读