美文网首页
java中json数据格式的处理

java中json数据格式的处理

作者: 时芥蓝 | 来源:发表于2016-11-30 18:28 被阅读216次

    json基础

    • json表示法是一种轻量级的基于文本的开放标准
    • jsonjavascript object notation的缩写
    • json的网络媒体格式是 application/json
    • 容易阅读和编写
    • 语言无关性

    json的几种数据格式

    • 对象

      An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by .

      object
    • 数组

    An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by ,

    array
    • A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

      value

    json语法

    • 数据使用键值对表示
    • 使用大括号表示对象,每个名称后面跟着一个:,键值对之间使用,分割
    • 使用方括号[]保存数组,数组值使用,分割
    {
        "book": [
            {
                "id":"01",
                "language": "Java",
                "edition": "third",
                "author": "Herbert Schildt"
            },
            {
                "id":"07",
                "language": "C++",
                "edition": "second"
                "author": "E.Balagurusamy"
        }]
    }
    

    book为键,它对应的又是一个json数组对象,json数组用[]包起来:

    [ 
        {
            "id":"01",
            "language": "Java",
            "edition": "third",
            "author": "Herbert Schildt"
        },
        {
            "id":"07",
            "language": "C++",
            "edition": "second"
            "author": "E.Balagurusamy"
        }
    ]
    

    数组元素是一个json对象,对象之间使用,隔开:

    { 
        "id":"01",
        "language": "Java",
        "edition": "third",
        "author": "Herbert Schildt"
    }
    

    java中使用json

    下载 json .jar

        /**
         * 测试生成json对象
         * @throws JSONException
         */
        @Test
        public void testBuildJsonObject() throws JSONException {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",1);
            jsonObject.put("name","shixu");
            jsonObject.put("age",23);
            jsonObject.put("id",2); //JsonObject底层是维护一个map,所以不能有重复的键,这里会把之前的值覆盖掉
            System.out.println(jsonObject);
            System.out.println("id:"+jsonObject.get("id"));
            System.out.println("name:"+jsonObject.get("name"));
            System.out.println("age:"+jsonObject.get("age"));
          
          
            HashMap map = new HashMap<>();
            map.put("id",1);
            map.put("name","shixu");
            JSONObject jsonObjectMap= new JSONObject(map);  //使用map初始化
            System.out.println(jsonObjectMap);
            System.out.println("id:"+jsonObjectMap.get("id"));
            System.out.println("name:"+jsonObjectMap.get("name"));
        }
    
    

    结果:

    {"id":2,"age":23,"name":"shixu"}
    id:2
    name:shixu
    age:23
    {"id":1,"name":"shixu"}
    id:1


       /**
         * 测试生成JSONArray ,用于存放jsonObject
         * @throws JSONException
         */
        @Test
        public void testBuildJsonArray() throws JSONException {
            JSONArray jsonArray = new JSONArray();
            HashMap map = new HashMap<>();
            map.put("id",1);
            map.put("name","shixu");
            JSONObject jsonObjectMap= new JSONObject(map);  //使用map初始化
            jsonArray.put(jsonObjectMap);
            jsonArray.put(jsonObjectMap);
            System.out.println(jsonArray);
            System.out.println(jsonArray.getJSONObject(0));
            System.out.println(jsonArray.getJSONObject(1));
        }
    

    结果:

    [{"id":1,"name":"shixu"},{"id":1,"name":"shixu"}]

    {"id":1,"name":"shixu"}
    {"id":1,"name":"shixu"}


    将一个json字符串解析为json对象

    /**
     * 将一个json字符串解析为json对象
     * @throws JSONException
     */
    @Test
    public void testJsonStrToJsonObject() throws JSONException {
        String jsonStr = "[{\"id\":1,\"name\":\"shixu\"},{\"id\":2,\"name\":\"suntime\"}]";
        JSONTokener jsonTokener = new JSONTokener(jsonStr);
        JSONArray jsonArray = new JSONArray(jsonTokener);
        for (int i = 0; i <jsonArray.length() ; i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            System.out.println(jsonObject);
            System.out.print("id:"+jsonObject.get("id"));
            System.out.println("\tname:"+jsonObject.get("name"));
        }
    }
    

    结果:

    {"id":1,"name":"shixu"}
    id:1 name:shixu
    {"id":2,"name":"suntime"}
    id:2 name:suntime

    总结

    相关文章

      网友评论

          本文标题:java中json数据格式的处理

          本文链接:https://www.haomeiwen.com/subject/kxmtmttx.html