java中json数据格式的处理

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

总结

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,949评论 18 139
  • 1.在C/C++中实现本地方法 生成C/C++头文件之后,你就需要写头文件对应的本地方法。注意:所有的本地方法的第...
    JayQiu阅读 2,406评论 0 3
  • Jni数据类型 Jni方法 来自 http://blog.chinaunix.net/uid-22028680-i...
    FlyDragonInSky阅读 935评论 0 0
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,775评论 0 9
  • 我死在平成27年。 那是日本的记时方法。 三月过后,我的灵魂游离了躯体,这身皮囊和一群所谓人类的生物乘着飞机来到了...
    江留白阅读 335评论 0 0