org.json的使用

本文详细介绍了如何使用org.json库进行JSON数据的处理,包括基本的JSONObject和JSONArray操作,以及复杂的集合处理技巧。通过实际代码示例展示了如何构建、解析JSON对象和数组,并提供了注意事项以避免常见错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jar包的下载到mvn里面去下载好了http://mvnrepository.com/artifact/org.json/json/20170516

直接上代码分析代码最好不过了?自己拿去运行分析一下就是你自己的东西了。


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 重点:JSONObject对应集合Map,JSONArray对应集合List.
 * 用json类(fastjson和orgjson)库处理复杂的集合时,要将集合直接放到value处,不能将其转换为json字符串放到value处,
 * 如果将其转换为字符串放到value处,最终生成的json字符串会带有转义字符.
 */
public class OrgjsonUtil {

    public static void main(String[] args) {
        try {
            //JSONObject的用法.
            System.out.println("-----test1-----");
            testJsonObject();

            //JSONArray的用法.
            System.out.println("-----test2-----");
            testJsonArray();

            //JSONObject,JSONArray和集合的复杂用法.
            System.out.println("-----test3-----");
            testComplex();

            //JSONObject,JSONArray和集合的复杂用法.
            System.out.println("-----test4-----");
            testComplex2();
        } catch (Exception e) {
            System.out.println("测试发生异常: " + e);
        }
    }

    public static void testJsonObject() throws JSONException {
        JSONObject jo = new JSONObject();
        jo.put("username", "IluckySi");
        jo.put("age", 27);
        jo.put("sex", true);
        Map<String, String> skill = new HashMap<String, String>();
        skill.put("java", "不错");
        skill.put("javascript", "凑合");
        skill.put("jquery", "挺好");
        jo.put("skill",skill);

        String username = jo.getString("username");
        int age = jo.getInt("age");
        boolean sex = jo.getBoolean("sex");

        JSONObject skill2 = new JSONObject(jo.getJSONObject("skill").toString());
        System.out.println(skill2);

        System.out.println(username + ", 年龄 = " + age + ", 性别 = " + (sex == true ? "男" : "女") + ", 技能如下:");
        /*Map<String, Object>  names = new HashMap<String, Object>();
        names = jo.getJSONObject("skill").toMap();
        for (String key : names.keySet()) {
             String value = names.get(key).toString();
            System.out.println("Key = " + key + ", Value = " + value);
        }
        */
        String[] names = JSONObject.getNames(skill2);
        for(String name : names) {
            System.out.println(name + ": " + skill2.getString(name));
        }

    }

    public static void testJsonArray() throws JSONException {
        JSONArray ja = new JSONArray();
        JSONObject jo = new JSONObject();
        jo.put("username", "IluckySi");
        jo.put("age", 27);
        jo.put("sex", true);
        ja.put(jo);
        JSONObject jo2 = new JSONObject();
        jo2.put("username", "IluckySi2");
        jo2.put("age", 28);
        jo2.put("sex", false);
        ja.put(jo2);
        for(int i = 0; i < ja.length(); i++) {
            JSONObject j = (JSONObject)ja.get(i);
            String username = j.getString("username");
            int age = j.getInt("age");
            boolean sex = j.getBoolean("sex");
            System.out.println(username + ", 年龄 = " + age + ", 性别 = " + (sex == true ? "男" : "女"));
        }

    }

    public static void testComplex() throws JSONException {
        JSONObject jo = new JSONObject();
        jo.put("result", "success");
        List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
        Map<Object, Object> user1 = new HashMap<Object, Object>();
        user1.put("name", "name1");
        user1.put("password", "password1");
        list.add(user1);
        Map<Object, Object> user2 = new HashMap<Object, Object>();
        user2.put("name", "name2");
        user2.put("password", "password2");
        list.add(user2);
        jo.put("message", list);
        String send = jo.toString();
        System.out.println("要测试的消息" + send);
        send(send);
    }

    private static void send(String send) throws JSONException {
        JSONObject jo = new JSONObject(send);
        String result = jo.getString("result");
        System.out.println("result=" + result);
        System.out.println(jo.getJSONArray("message"));
        JSONArray ja = jo.getJSONArray("message");
        for(int i = 0; i < ja.length(); i++) {
            JSONObject j = (JSONObject)ja.get(i);
            String name = j.getString("name");
            String password = j.getString("password");
            System.out.println("name = " + name + ", password = " + password);
        }
    }

    public static void testComplex2() throws JSONException {
        JSONObject jo = new JSONObject();
        jo.put("result", "success");
        JSONObject message = new JSONObject();
        List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
        Map<Object, Object> user1 = new HashMap<Object, Object>();
        user1.put("name", "name1");
        user1.put("password", "password1");
        list.add(user1);
        Map<Object, Object> user2 = new HashMap<Object, Object>();
        user2.put("name", "name2");
        user2.put("password", "password2");
        list.add(user2);
        message.put("message", list);
        jo.put("message", message.toString());//注意:如果直接写message,也不会有转义字符.
        String send = jo.toString();
        System.out.println("要测试的消息" + send);
        send2(send);
    }

    private static void send2(String send) throws JSONException {
        JSONObject jo = new JSONObject(send);
        String result = jo.getString("result");
        System.out.println("result=" + result);
        System.out.println(jo.getString("message").toString());
        JSONObject message = new JSONObject(jo.getString("message"));
        JSONArray message2 = message.getJSONArray("message");

        for(int i = 0; i < message2.length(); i++) {
            JSONObject j = (JSONObject)message2.get(i);
            String name = j.getString("name");
            String password = j.getString("password");
            System.out.println("name = " + name + ", password = " + password);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值