Java中JSON的操作

来源:互联网 发布:淘宝客服昵称怎么改 编辑:程序博客网 时间:2024/04/30 08:28

简介

JSON:JavaScript对象表示法(JavaScript Object Notation)。
是储存和交换文本信息的语法。

  • JSON是轻量级的文本数据交换格式。
  • JSON独立于语言和平台。
  • JSON具有自我描述性,更易理解。

JSON与XML
类似XML,比XML更小、更快、更易解析。
- 没有结束标签
- 更短
- 读写速度更快
- 使用数组
- 不使用保留字

JSON语法
JSON语法是JavaScript对象表示法语法的子集。

  • 数据在名称/值对中。
  • 数据由逗号分隔。
  • 花括号保存对象。
  • 方括号保存数组。

JSON的值可以是:

  • 数字(整数或浮点数)
  • 字符串(在双引号中)
  • 逻辑值(true或false)
  • 数组
  • 对象
  • NULL

比如:
JSON对象

{"firstName":"John","lastName":"Doe"}

JSON数组在方括号中书写,数组中可以包含对个对象。

{    "employees":[        {"firstName":"John","lastName":"Doe"},        {"firstName":"Anna","lastName":"Smith"},        {"firstName":"Peter","lastName":"Johns"}        ]}

在Java中读取和创建JSON数据
在读取时首先要创建JSON解析器,用JsonParser类来创建,然后用它可以来解析文件或者字符串中的json数据。
举个例子:

{"cat":"it",    "languages":[        {"id":1,"name":"Java","ide":"Eclipse"},        {"id":2,"name":"Swift","ide":"XCode"},        {"id":3,"name":"C#","ide":"Vistual Studio"}        ],        "pop":true}

用java来解析这段JSON数据:

import java.io.FileNotFoundException;import java.io.FileReader;import com.google.gson.JsonArray;import com.google.gson.JsonIOException;import com.google.gson.JsonObject;import com.google.gson.JsonParser;import com.google.gson.JsonSyntaxException;public class ReadJson {    public static void main(String[] args) {        try {            //JSON解析器            JsonParser parser = new JsonParser();            JsonObject object = (JsonObject) parser.parse(new FileReader("test.json"));            System.out.println("cat=" + object.get("cat").getAsString());            System.out.println("pop=" + object.get("pop").getAsBoolean());            JsonArray array = object.get("languages").getAsJsonArray();            for (int i = 0; i < array.size(); i++) {                System.out.println("---------");                JsonObject subObject = array.get(i).getAsJsonObject();                System.out.println("id=" + subObject.get("id").getAsInt());                System.out.println("name=" + subObject.get("name").getAsString());                System.out.println("ide=" + subObject.get("ide").getAsString());            }        } catch (JsonIOException e) {            e.printStackTrace();        } catch (JsonSyntaxException e) {            e.printStackTrace();        } catch (FileNotFoundException e) {            e.printStackTrace();        }    }}

创建JSON,创建时通过json对象或者json数组来完成,比如要创建上述的JSON数据:
程序:

import com.google.gson.JsonArray;import com.google.gson.JsonObject;public class CreateJSON {    public static void main(String[] args) {        JsonObject object = new JsonObject();        object.addProperty("cat", "it");        JsonArray array = new JsonArray();        JsonObject lan1 = new JsonObject();        lan1.addProperty("id", 1);        lan1.addProperty("name", "Java");        lan1.addProperty("ide", "Eclipse");        JsonObject lan2 = new JsonObject();        lan2.addProperty("id", 2);        lan2.addProperty("name", "Swift");        lan2.addProperty("ide", "XCode");        JsonObject lan3 = new JsonObject();        lan3.addProperty("id", 3);        lan3.addProperty("name", "C#");        lan3.addProperty("ide", "Vistual Studio");        array.add(lan1);        array.add(lan2);        array.add(lan3);        object.add("languages", array);        object.addProperty("pop", true);        System.out.println(object.toString());    }}

这就是一个简单的在json介绍和在Java中的操作。

1 0
原创粉丝点击