美文网首页
Java Gson 教程

Java Gson 教程

作者: zqyadam | 来源:发表于2021-11-24 23:05 被阅读0次

    原文:https://geek-docs.com/java/java-tutorial/gson.html

    Gson教程展示了如何使用Gson库在Java中使用JSON。我们使用三种不同的Gson API来处理JSON。源代码可在作者的Github存储库中获得。

    JSON(JavaScript对象表示法)是一种轻量级的数据交换格式。人类很容易读写,机器也很容易解析和生成。与XML相比,它不那么冗长且更具可读性。JSON的官方Internet媒体类型为application/jsonJSON文件扩展名是.jsonJSON可直接由JavaScript使用。

    Java Gson 库

    GsonJava序列化/反序列化库,用于将Java对象转换为JSON并返回。GsonGoogle创建,供内部使用,后来开源。

    Java Gson Maven 依赖

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.2</version>
    </dependency>
    

    这是对GsonMaven依赖。

    Java Gson 特性

    这些是Gson特性:

    • 用于Java对象JSON序列化和反序列化的简单工具。
    • Java泛型的广泛支持。
    • 对象的自定义表示。
    • 支持任意复杂的对象。
    • 快速,低内存占用。
    • 允许紧凑的输出和漂亮的打印。

    Java Gson API

    Gson具有三种API

    • 数据绑定API
    • 树模型API
    • API

    数据绑定API使用属性访问器将JSONPOJO之间进行转换。Gson使用数据类型适配器处理JSON数据。它类似于XML JAXB解析器。

    树模型API创建JSON文档的内存树表示。它构建JsonElements的树。它类似于XML DOM解析器。

    API是一种低级API,它使用JsonReaderJsonWriter作为离散记号读取和写入JSON内容。这些类将数据读取为JsonTokens。该API具有最低的开销,并且在读/写操作中速度很快。它类似于XMLStax解析器。

    Java Gson 类

    Gson是使用Gson库的主要类。 有两种创建Gson的基本方法:

    • Gson()
    • 新的GsonBuilder().create()

    GsonBuilder可用于使用各种配置设置来构建Gson

    Java GsontoJson()

    toJson()方法将指定的对象序列化为其等效的JSON表示形式。

    GsonToJson.java
    package com.zetcode;
    
    import com.google.gson.Gson;
    import java.util.HashMap;
    import java.util.Map;
    
    public class GsonToJson {
    
        public static void main(String[] args) {
    
            Map<Integer, String> colours = new HashMap<>();
            colours.put(1, "blue");
            colours.put(2, "yellow");
            colours.put(3, "green");
    
            Gson gson = new Gson();
    
            String output = gson.toJson(colours);
    
            System.out.println(output);
        }
    }
    

    在示例中,我们使用toJSon()方法将映射序列化为JSON

    {"1":"blue","2":"yellow","3":"green"}
    

    这是示例的输出。

    Java GsonfromJson()

    fromJson()方法将指定的JSON反序列化为指定类的对象。

    GsonFromJson.java

    package com.zetcode;
    
    import com.google.gson.Gson;
    
    class User {
    
        private final String firstName;
        private final String lastName;
    
        public User(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        @Override
        public String toString() {
            return new StringBuilder().append("User{").append("First name: ")
                    .append(firstName).append(", Last name: ")
                    .append(lastName).append("}").toString();
        }
    }
    
    public class GsonFromJson {
    
        public static void main(String[] args) {
    
            String json_string = "{\"firstName\":\"Tom\", \"lastName\": \"Broody\"}";
    
            Gson gson = new Gson();
            User user = gson.fromJson(json_string, User.class);
    
            System.out.println(user);
        }
    }
    

    该示例使用fromJson()方法将JSON读取到Java对象中。

    class User {
    
        private final String firstName;
        private final String lastName;
    
        public User(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        @Override
        public String toString() {
            return new StringBuilder().append("User{").append("First name: ")
                    .append(firstName).append(", Last name: ")
                    .append(lastName).append("}").toString();
        }
    }
    

    注意,没有必要使用gettersetter方法。

    User{First name: Tom, Last name: Broody}
    

    This is the output of the example.

    GsonBuilder

    GsonBuilder使用各种配置设置构建GsonGsonBuilder遵循构建器模式,通常通过首先调用各种配置方法来设置所需的选项,最后调用create()来使用它。

    GsonBuilderEx.java

    package com.zetcode;
    
    import com.google.gson.FieldNamingPolicy;
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import java.io.IOException;
    import java.io.PrintStream;
    
    class User {
    
        private final String firstName;
        private final String lastName;
    
        public User(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }
    
    public class GsonBuilderEx {
    
        public static void main(String[] args) throws IOException {
    
            try (PrintStream prs = new PrintStream(System.out, true, 
                    "UTF8")) {
    
                Gson gson = new GsonBuilder()
                        .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
                        .create();
    
                User user = new User("Peter", "Flemming");
                gson.toJson(user, prs);
            }
        }
    }
    

    在示例中,我们将对象写入JSON。 我们使用GsonBuilder创建Gson

    Gson gson = new GsonBuilder()
            .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
            .create();
    

    我们使用GsonBuilder创建并配置Gson。字段命名策略设置为FieldNamingPolicy.UPPER_CAMEL_CASE

    {"FirstName":"Peter","LastName":"Flemming"}
    

    这是输出。

    Java Gson 漂亮打印

    Gson有两种输出模式:紧凑和漂亮。

    GsonPrettyPrinting.java

    package com.zetcode;
    
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import java.util.HashMap;
    import java.util.Map;
    
    public class GsonPrettyPrinting {
    
        public static void main(String[] args) {
    
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
    
            Map<String, Integer> items = new HashMap<>();
    
            items.put("chair", 3);
            items.put("pencil", 1);
            items.put("book", 5);
    
            gson.toJson(items, System.out);
        }
    }
    

    该示例漂亮地显示了JSON输出。

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    

    setPrettyPrinting()方法设置漂亮的打印模式。

    {
      "chair": 3,
      "book": 5,
      "pencil": 1
    }
    

    This is the output of the example.

    序列化空值

    默认情况下,Gson不会将具有空值的字段序列化为JSON。如果Java对象中的字段为null,则Gson会将其排除。我们可以使用serializeNulls()方法强制 Gson通过GsonBuilder序列化null值。

    GsonSerializeNulls.java

    package com.zetcode;
    
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    
    class User {
    
        private String firstName;
        private String lastName;
    
        public User() {};
    
        public User(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        @Override
        public String toString() {
            return new StringBuilder().append("User{").append("First name: ")
                    .append(firstName).append(", Last name: ")
                    .append(lastName).append("}").toString();
        }
    }
    
    public class GsonSerializeNulls {
    
        public static void main(String[] args) {
    
            GsonBuilder builder = new GsonBuilder();
    
            builder.serializeNulls();
    
            Gson gson = builder.create();
    
            User user = new User();
            user.setFirstName("Norman");
    
            String json = gson.toJson(user);
            System.out.println(json);
    
        }
    }
    

    该示例显示了如何序列化null值。

    {"firstName":"Norman","lastName":null}
    

    This is the output.

    Java Gson 写入列表

    以下示例将JSON对象列表写入文件。

    GsonWriteList.java

    package com.zetcode;
    
    import com.google.gson.Gson;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.nio.charset.StandardCharsets;
    import java.util.ArrayList;
    import java.util.List;
    
    class Item {
    
        private final String name;
        private final int quantity;
    
        public Item(String name, int quantity) {
            this.name = name;
            this.quantity = quantity;
        }
    }
    
    public class GsonWriteList {
    
        public static void main(String[] args) throws IOException {
    
            String fileName = "src/main/resources/items.json";
    
            try (FileOutputStream fos = new FileOutputStream(fileName);
                    OutputStreamWriter isr = new OutputStreamWriter(fos, 
                            StandardCharsets.UTF_8)) {
    
                Gson gson = new Gson();
    
                Item item1 = new Item("chair", 4);
                Item item2 = new Item("book", 5);
                Item item3 = new Item("pencil", 1);
    
                List<Item> items = new ArrayList<>();
                items.add(item1);
                items.add(item2);
                items.add(item3);
    
                gson.toJson(items, isr);
            }
    
            System.out.println("Items written to file");
        }
    }
    

    该示例将JSON数据写入items.json文件。

    Java Gson 读入数组

    下一个示例将数据读取到Java数组中。

    $ cat users.json
    [{"firstName":"Peter","lastName":"Flemming"}, {"firstName":"Nicole","lastName":"White"},
         {"firstName":"Robin","lastName":"Bullock"} ]
    

    这些是users.json文件的内容。

    GsonReadArray.java

    package com.zetcode;
    
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import java.io.File;
    import java.io.IOException;
    import java.io.Reader;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.util.Arrays;
    
    class User {
    
        private final String firstName;
        private final String lastName;
    
        public User(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        @Override
        public String toString() {
            return new StringBuilder().append("{User").append("First name: ")
                    .append(firstName).append(", Last name: ")
                    .append(lastName).append("}").toString();
        }
    }
    
    public class GsonReadArray {
    
        public static void main(String[] args) throws IOException {
    
            Gson gson = new GsonBuilder().create();
    
            String fileName = "src/main/resources/users.json";
            Path path = new File(fileName).toPath();
    
            try (Reader reader = Files.newBufferedReader(path, 
                    StandardCharsets.UTF_8)) {
    
                User[] users = gson.fromJson(reader, User[].class);
    
                Arrays.stream(users).forEach( e -> {
                    System.out.println(e);
                });
            }
        }
    }
    

    该示例将items.json文件中的数据读取到数组中。 我们将数组的内容打印到控制台。

    User[] users = gson.fromJson(reader, User[].class);
    

    fromJson()的第二个参数是数组类。

    Java Gson从URL读取JSON

    以下示例从网页读取JSON数据。我们从http://time.jsontest.com获得JSON数据。

    {
       "time": "02:44:19 PM",
       "milliseconds_since_epoch": 1496155459478,
       "date": "05-30-2017"
    }
    

    GET请求返回此JSON字符串。

    GsonReadWebPage.java

    package com.zetcode;
    
    import com.google.gson.Gson;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.net.URL;
    import java.nio.charset.StandardCharsets;
    
    class TimeData {
    
        private String time;
        private Long milliseconds_since_epoch;
        private String date;
    
        @Override
        public String toString() {
            return "TimeData{" + "time=" + time + ", milliseconds_since_epoch="
                    + milliseconds_since_epoch + ", date=" + date + '}';
        }
    }
    
    public class GsonReadWebPage {
    
        public static void main(String[] args) throws IOException {
    
            String webPage = "http://time.jsontest.com";
    
            try (InputStream is = new URL(webPage).openStream();
                    Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
    
                Gson gson = new Gson();
                TimeData td = gson.fromJson(reader, TimeData.class);
    
                System.out.println(td);
            }
        }
    }
    

    该代码示例从http://time.jsontest.com读取JSON数据。

    TimeData{time=11:23:09 PM, milliseconds_since_epoch=1516317789302, date=01-18-2018}
    

    This is the output.

    Java Gson使用@Expose排除字段

    @Expose注解指示应公开成员以进行JSON序列化或反序列化。 @Expose注解可以采用两个布尔参数:serializedeserialize。 必须使用excludeFieldsWithoutExposeAnnotation()方法显式启用@Expose注解。

    GsonExcludeFields.java

    package com.zetcode;
    
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import com.google.gson.annotations.Expose;
    
    enum MaritalStatus {
    
        SINGLE,
        MARRIED,
        DIVORCED,
        UNKNOWN
    }
    
    class Person {
    
        @Expose
        private String firstName;
    
        @Expose
        private String lastName;
    
        private MaritalStatus maritalStatus;
    
        public Person(String firstName, String lastName, 
                MaritalStatus maritalStatus) {
    
            this.firstName = firstName;
            this.lastName = lastName;
            this.maritalStatus = maritalStatus;
        }
    
        public Person() {}
    }
    
    public class GsonExcludeFields {
    
        public static void main(String[] args) {
    
            Gson gson = new GsonBuilder()
                    .excludeFieldsWithoutExposeAnnotation()
                    .setPrettyPrinting()
                    .create();
    
            Person p = new Person("Jack", "Sparrow", MaritalStatus.UNKNOWN);
    
            gson.toJson(p, System.out);        
        }
    }
    

    在示例中,我们从序列化中排除一个字段。

    @Expose
    private String firstName;
    
    @Expose
    private String lastName;
    
    private MaritalStatus maritalStatus;
    

    婚姻状况字段不会被序列化,因为它没有用@Expose注解修饰。

    Gson gson = new GsonBuilder()
            .excludeFieldsWithoutExposeAnnotation()
            .setPrettyPrinting()
            .create();
    

    @Expose注解通过excludeFieldsWithoutExposeAnnotation()方法启用了字段排除。

    {
      "firstName": "Jack",
      "lastName": "Sparrow"
    }
    

    This is the output.

    Java Gson 数据绑定 API

    数据绑定API使用属性访问器在POJOJSON之间进行转换。Gson使用数据类型适配器处理JSON数据。

    Gson 数据绑定 API 编写

    在下面的示例中,我们使用数据绑定API编写数据。

    GsonDataBindApiWrite.java

    package com.zetcode;
    
    import com.google.gson.Gson;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.Writer;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.ArrayList;
    import java.util.List;
    
    class Car {
    
        private final String name;
        private final String model;
        private final int price;
        private final String[] colours;
    
        public Car(String name, String model, int price, String[] colours) {
            this.name = name;
            this.model = model;
            this.price = price;
            this.colours = colours;
        }
    }
    
    public class GsonDataBindApiWrite {
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
    
            List<Car> cars = new ArrayList<>();
            cars.add(new Car("Audi", "2012", 22000, new String[]{"gray", "red", "white"}));
            cars.add(new Car("Skoda", "2016", 14000, new String[]{"black", "gray", "white"}));
            cars.add(new Car("Volvo", "2010", 19500, new String[]{"black", "silver", "beige"}));
    
            String fileName = "src/main/resources/cars.json";
            Path path = Paths.get(fileName);
    
            try (Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
    
                Gson gson = new Gson();
                gson.toJson(cars, writer);
            }
    
            System.out.println("Cars written to file");
        }
    }
    

    在示例中,我们创建了一个汽车对象列表,并使用Gson数据绑定API对其进行了序列化。

    Gson gson = new Gson();
    gson.toJson(cars, writer);
    

    我们将cars列表传递给toJson()方法。Gson自动将汽车对象映射到JSON

    读取Gson数据绑定API

    在下面的示例中,我们使用数据绑定API读取数据。

    GsonDataBindingApiRead.java

    package com.zetcode;
    
    import com.google.gson.Gson;
    import com.google.gson.reflect.TypeToken;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.Reader;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.Arrays;
    import java.util.List;
    
    class Car {
    
        private final String name;
        private final String model;
        private final int price;
        private final String[] colours;
    
        public Car(String name, String model, int price, String[] colours) {
            this.name = name;
            this.model = model;
            this.price = price;
            this.colours = colours;
        }
    
        @Override
        public String toString() {
            return "Car{" + "name=" + name + ", model=" + model + 
                    ", price=" + price + ", colours=" + Arrays.toString(colours) + '}';
        }
    }
    
    public class GsonDataBindingApiRead {
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
    
            String fileName = "src/main/resources/cars.json";
            Path path = Paths.get(fileName);
    
            try (Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
    
                Gson gson = new Gson();
                List<Car> cars = gson.fromJson(reader, 
                        new TypeToken<List<Car>>(){}.getType());
    
                cars.forEach(System.out::println);
            }        
        }
    }
    

    在示例中,我们使用Gson数据绑定API将数据从JSON文件读取到汽车对象列表中。

    List<Car> cars = gson.fromJson(reader, 
            new TypeToken<List<Car>>(){}.getType());
    

    Gson自动将JSON映射到Car对象。由于类型信息在运行时会丢失,因此我们需要使用TypeTokenGson知道我们使用的是哪种类型。

    Java Gson树模型 API

    树模型API在内存中创建JSON文档的树表示。它构建JsonElements的树。JsonElement是代表Json元素的类。它可以是JsonObjectJsonArrayJsonPrimitiveJsonNull

    Gson树模型写

    在以下示例中,我们使用Gson树模型APIJava对象写入JSON

    GsonTreeModelWrite.java

    package com.zetcode;
    
    import com.google.gson.Gson;
    import com.google.gson.JsonArray;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.Writer;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.ArrayList;
    import java.util.List;
    
    class Car {
    
        private final String name;
        private final String model;
        private final int price;
        private final String[] colours;
    
        public Car(String name, String model, int price, String[] colours) {
            this.name = name;
            this.model = model;
            this.price = price;
            this.colours = colours;
        }
    }
    
    public class GsonTreeModelWrite {
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
    
            List<Car> cars = new ArrayList<>();
            cars.add(new Car("Audi", "2012", 22000, 
                    new String[]{"gray", "red", "white"}));
            cars.add(new Car("Skoda", "2016", 14000, 
                    new String[]{"black", "gray", "white"}));
            cars.add(new Car("Volvo", "2010", 19500, 
                    new String[]{"black", "silver", "beige"}));
    
            String fileName = "src/main/resources/cars.json";
            Path path = Paths.get(fileName);
    
            try (Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
    
                Gson gson = new Gson();
    
                JsonElement tree = gson.toJsonTree(cars);
                gson.toJson(tree, writer);
            }
    
            System.out.println("Cars written to file");
        }        
    }
    

    汽车对象列表被序列化为JSON格式。

    JsonElement tree = gson.toJsonTree(cars);
    

    toJsonTree方法将指定的对象序列化为其等效表示形式,作为JsonElements的树。

    JsonArray jarray = tree.getAsJsonArray();
    

    我们使用getAsJsonArray()方法将树转换为JsonArray

    JsonElement jel = jarray.get(1);
    

    我们从数组中获取第二个元素。

    JsonObject object = jel.getAsJsonObject();
    object.addProperty("model", "2009");
    

    我们修改一个属性。

    gson.toJson(tree, writer);
    

    最后,我们将树对象写入文件中。

    Gson树模型读取

    在以下示例中,我们使用Gson树模型APIJSON读取Java对象。

    cars.json

    [{"name":"Audi","model":"2012","price":22000,"colours":["gray","red","white"]},
     {"name":"Skoda","model":"2009","price":14000,"colours":["black","gray","white"]},
     {"name":"Volvo","model":"2010","price":19500,"colours":["black","silver","beige"]}]
    

    这是cars.json文件中的JSON数据。

    GsonTreeModelRead.java

    package com.zetcode;
    
    import com.google.gson.JsonArray;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.Reader;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    public class GsonTreeModelRead {
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
    
            String fileName = "src/main/resources/cars.json";
            Path path = Paths.get(fileName);
    
            try (Reader reader = Files.newBufferedReader(path, 
                    StandardCharsets.UTF_8)) {
    
                JsonParser parser = new JsonParser();
                JsonElement tree = parser.parse(reader);
    
                JsonArray array = tree.getAsJsonArray();
    
                for (JsonElement element : array) {
    
                    if (element.isJsonObject()) {
    
                        JsonObject car = element.getAsJsonObject();
    
                        System.out.println("********************");
                        System.out.println(car.get("name").getAsString());
                        System.out.println(car.get("model").getAsString());
                        System.out.println(car.get("price").getAsInt());
    
                        JsonArray cols = car.getAsJsonArray("colors");
    
                        cols.forEach(col -> {
                            System.out.println(col);
                        });
                    }
                }
            }
        }
    }
    

    在示例中,我们将JSON数据从文件读取到JsonElements树中。

    JsonParser parser = new JsonParser();
    JsonElement tree = parser.parse(reader);
    

    JsonParserJSON解析为JsonElements的树结构。

    JsonArray array = tree.getAsJsonArray();
    

    我们将树作为JsonArray

    for (JsonElement element : array) {
    
        if (element.isJsonObject()) {
    
            JsonObject car = element.getAsJsonObject();
    
            System.out.println("********************");
            System.out.println(car.get("name").getAsString());
            System.out.println(car.get("model").getAsString());
            System.out.println(car.get("price").getAsInt());
    
            JsonArray cols = car.getAsJsonArray("colors");
    
            cols.forEach(col -> {
                System.out.println(col);
            });
        }
    }
    

    我们浏览JsonArray并打印其元素的内容。

    Java Gson流API

    GsonAPI是一个低级API,它以离散记号(JsonTokens)的形式读取和写入 JSON。 主要类别是JsonReaderJsonWriterJsonTokenJSON编码的字符串中的结构,名称或值类型。

    这些是JsonToken类型:

    • BEGIN_ARRAY — 打开JSON数组
    • END_ARRAY — 关闭JSON数组
    • BEGIN_OBJECT — 打开JSON对象
    • END_OBJECT — 关闭JSON对象
    • NAME - JSON属性名称
    • STRINGJSON字符串
    • NUMBERJSON数字(双精度,长整型或整型)
    • BOOLEANJSON布尔值
    • NULLJSON空值
    • END_DOCUMENTJSON流的末尾。

    JsonWriter

    JsonWriterJSON编码值写入流,一次写入一个记号。 流包含文字值(字符串,数字,布尔值和null)以及对象和数组的开始和结束定界符。每个JSON文档必须包含一个顶级数组或对象。

    使用beginObject()endObject()方法调用创建对象。在对象内,标记在名称及其值之间交替。 在beginArray()endArray()方法调用中创建数组。

    GsonStreamApiWrite.java

    package com.zetcode;
    
    import com.google.gson.stream.JsonWriter;
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    public class GsonStreamApiWrite {
    
        public static void main(String[] args) throws IOException {
    
            String fileName = "src/main/resources/cars.json";
            Path path = Paths.get(fileName);
    
            try (JsonWriter writer = new JsonWriter(Files.newBufferedWriter(path, 
                    StandardCharsets.UTF_8))) {
    
                writer.beginObject(); 
                writer.name("name").value("Audi");
                writer.name("model").value("2012");
                writer.name("price").value(22000);
    
                writer.name("colours");
                writer.beginArray();
                writer.value("gray");
                writer.value("red");
                writer.value("white");
                writer.endArray();
    
                writer.endObject();
            }
    
            System.out.println("Data written to file");
        }
    }
    

    在示例中,我们将一个汽车对象写入JSON文件。

    try (JsonWriter writer = new JsonWriter(Files.newBufferedWriter(path, 
            StandardCharsets.UTF_8))) {
    

    创建一个新的JsonWriter

    writer.beginObject(); 
    ...
    writer.endObject();
    

    如上所述,每个JSON文档必须具有一个顶级数组或对象。 在我们的例子中,我们有一个顶级对象。

    writer.name("name").value("Audi");
    writer.name("model").value("2012");
    writer.name("price").value(22000);
    

    我们将键值对写入文档。

    writer.name("colours");
    writer.beginArray();
    writer.value("gray");
    writer.value("red");
    writer.value("white");
    writer.endArray();
    

    在这里,我们创建一个数组。

    JsonReader

    JsonReader读取JSON编码值作为记号流。

    GsonStreamApiRead.java

    package com.zetcode;
    
    import com.google.gson.stream.JsonReader;
    import com.google.gson.stream.JsonToken;
    import java.io.IOException;
    import java.io.StringReader;
    
    public class GsonStreamApiRead {
    
        public static void main(String[] args) throws IOException {
    
            String json_string = "{\"name\":\"chair\",\"quantity\":3}";
    
            try (JsonReader reader = new JsonReader(new StringReader(json_string))) {
    
                while (reader.hasNext()) {
    
                    JsonToken nextToken = reader.peek();
    
                    if (JsonToken.BEGIN_OBJECT.equals(nextToken)) {
    
                        reader.beginObject();
    
                    } else if (JsonToken.NAME.equals(nextToken)) {
    
                        reader.nextName();
    
                    } else if (JsonToken.STRING.equals(nextToken)) {
    
                        String value = reader.nextString();
                        System.out.format("%s: ", value);
    
                    } else if (JsonToken.NUMBER.equals(nextToken)) {
    
                        long value = reader.nextLong();
                        System.out.println(value);
    
                    }
                }
            }
        }
    }
    

    该示例使用JsonReaderJSON字符串读取数据。

    JsonReader reader = new JsonReader(new StringReader(json_string));
    

    JsonReader对象已创建。它从JSON字符串读取。

    while (reader.hasNext()) {
    

    while循环中,我们迭代流中的记号。

    JsonToken nextToken = reader.peek();
    

    我们使用peek()方法获得下一个标记的类型。

    reader.beginObject();
    

    beginObject()方法使用JSON流中的下一个记号,并断言它是新对象的开始。

    reader.nextName();
    

    nextName()方法返回下一个JsonToken并使用它。

    String value = reader.nextString();
    System.out.format("%s: ", value);
    

    我们获取下一个字符串值并将其打印到控制台。

    在本教程中,我们展示了如何通过Gson库使用JSON

    相关文章

      网友评论

          本文标题:Java Gson 教程

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