Json to Gson - Modeling

删除回忆录丶 提交于 2019-12-30 07:31:29

问题


I'm trying to convert a JSON to GSON , but I can not model. Can anyone give me an example with this one.

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

Thanks


回答1:


Here you go.

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;
}


来源:https://stackoverflow.com/questions/6300920/json-to-gson-modeling

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!