org.json的使用

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

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

org.json的使用_第1张图片

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 skill = new HashMap();
        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  names = new HashMap();
        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> list = new ArrayList>();
        Map user1 = new HashMap();
        user1.put("name", "name1");
        user1.put("password", "password1");
        list.add(user1);
        Map user2 = new HashMap();
        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> list = new ArrayList>();
        Map user1 = new HashMap();
        user1.put("name", "name1");
        user1.put("password", "password1");
        list.add(user1);
        Map user2 = new HashMap();
        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);
        }
    }
}


你可能感兴趣的:(java)