Json 到 Gson - 建模

标签 json model gson

我正在尝试将 JSON 转换为 GSON ,但我无法建模。谁能给我举个例子吗?

[
    {
        "id": "1",
        "name": "lalala",
        "object1": [
            "string1",
            "string1",
            "string1"
        ],
        "object2": [
            "anotherString1",
            "anotherString2"
        ]
    },
    {
        "id": "2",
        "name": "laaaaalala",
        "object1": [
            "string1",
            "string1",
            "string1"
        ],
        "object2": [
            "anotherString1",
            "anotherString2"
        ]
    }
]

谢谢

最佳答案

给你。

import java.io.FileReader;
import java.util.List;

import com.google.gson.Gson;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();
    Thing[] things = gson.fromJson(new FileReader("input.json"), Thing[].class);
    System.out.println(gson.toJson(things));
  }
}

class Thing
{
  String id;
  String name;
  String[] object1;
  List<String> object2;
}

关于Json 到 Gson - 建模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6300920/

相关文章:

ExtJs 模型代理与商店代理

android - 在 Kotlin 中使用 Gson 解析 JSON 数组

javascript - jQuery Flot - 四舍五入 x 轴值?

json - 如何在 circe 中覆盖默认编解码器?

ruby-on-rails - ActiveRecord 模型测试的性能

javascript - Kyle Simpson 的 OLOO 在 Object.create 之后添加多个属性

java - 通过 gson 解析带有混合子对象的 json 并作为列表发回

java - 使用 Gson 进行 JSON 反序列化

json - 从JSON删除字符

javascript - 查找子对象的最大值