Json to Gson - Modeling

后端 未结 1 1519
太阳男子
太阳男子 2021-01-07 00:12

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\": \         


        
相关标签:
1条回答
  • 2021-01-07 00:39

    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;
    }
    
    0 讨论(0)
提交回复
热议问题