Parsing JSON array with gson

后端 未结 1 1669
走了就别回头了
走了就别回头了 2021-01-06 21:44

I am having trouble parsing my JSON which i get from javascript. The format of JSON is this:

[{\"positions\":[{\"x\":50,\"y\":50},{\"x\":82,\"y\":50},{\"x\":         


        
相关标签:
1条回答
  • 2021-01-06 22:21

    Define your classes and you will get everything you need using gson:

    public class Class1 {
      private int x;
      private List<Class2> elements;
    }
    

    And the inner class:

    public class Class2 {
      private String str1;
      private Integer int2;
    }
    

    Now you can parse a json string of the outer class just like that:

    gson.fromJson(jsonString, Class1.class);
    

    Your error while using Gson is that you try to parse a complex object in String, which is not possible.

    0 讨论(0)
提交回复
热议问题