Json to Gson - 建模 [英] Json to Gson - Modeling

查看:104
本文介绍了Json to Gson - 建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将JSON转换为GSON,但我无法建模。任何人都可以给我一个这个例子。

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

推荐答案


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 to Gson - 建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆