使用Gson创建对象数组

时间:2014-06-02 17:26:03

标签: java json gson

我想将这个JSON字符串转换为可以用Java操作的对象数组。

String jsonExample = "" +
            "{"c":[{"v":"03/10/2013"},{"v":23},{"v":112}]},"+
            "{"c":[{"v":"04/10/2013"},{"v":14},{"v":232}]},"+
            "{"c":[{"v":"05/10/2013"},{"v":18},{"v":145}]},"+
            "{"c":[{"v":"06/10/2013"},{"v":22},{"v":211}]},"+
            "{"c":[{"v":"07/10/2013"},{"v":18},{"v":198}]},"+
            "{"c":[{"v":"08/10/2013"},{"v":15},{"v":215}]}";

首先,我放弃它:

String unescape = StringEscapeUtils.unescapeHtml4(jsonExample2);

然后,我尝试使用Gson,

Gson gson = new Gson();
ViewsHistoric viewHistoric = gson.fromJson(unescape, ViewsHistoric.class);

使用以下类:

    public static class ViewsHistoric {
    private List<Row> r;
    public List<Row> getR() {return r;}
    public ViewsHistoric() {}
}

public static class Row {
    private Triplet c;
    public Triplet getC() {return c;}
    public Row() {}
}

public static class Triplet {
    private String v;

    public String getV() {return v; }
    public Triplet() {}
}

但是我收到了一个错误:

  

java.lang.IllegalStateException:预期为BEGIN_OBJECT但在第1行第2列为STRING

嗯,实际上我在过去的几个小时里和Gson一起尝试了许多其他课程组合,但没有运气。

有人能告诉我如何将json字符串转换为java对象吗?

2 个答案:

答案 0 :(得分:1)

两件事:

  1. 您的JSON格式无效(即使在取消后也是如此)。
  2. 您的Triplet类是一个对象,而不是JSON数组。
  3. 我对您的代码进行了一些修改,以使其正常工作。这里有一个有效的例子

    public class Q24000071 {
    
        public static void main(String[] args) {
                 //the 'r' attribute, corresponding to 'ViewsHistoric' class, was missing
            String jsonExample = "{\"r\": ["
                    + "{&#34;c&#34;:[{&#34;v&#34;:&#34;03/10/2013&#34;},{&#34;v&#34;:23},{&#34;v&#34;:112}]},"
                    + "{&#34;c&#34;:[{&#34;v&#34;:&#34;04/10/2013&#34;},{&#34;v&#34;:14},{&#34;v&#34;:232}]},"
                    + "{&#34;c&#34;:[{&#34;v&#34;:&#34;05/10/2013&#34;},{&#34;v&#34;:18},{&#34;v&#34;:145}]},"
                    + "{&#34;c&#34;:[{&#34;v&#34;:&#34;06/10/2013&#34;},{&#34;v&#34;:22},{&#34;v&#34;:211}]},"
                    + "{&#34;c&#34;:[{&#34;v&#34;:&#34;07/10/2013&#34;},{&#34;v&#34;:18},{&#34;v&#34;:198}]},"
                    + "{&#34;c&#34;:[{&#34;v&#34;:&#34;08/10/2013&#34;},{&#34;v&#34;:15},{&#34;v&#34;:215}]}"
                    + "]}";
    
            String unescape = StringEscapeUtils.unescapeHtml4(jsonExample);
            System.out.println(unescape);
    
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            ViewsHistoric viewHistoric = gson.fromJson(unescape,
                    ViewsHistoric.class);
            System.out.println(gson.toJson(viewHistoric));
        }
    
        public static class ViewsHistoric {
    
            private List<Row> r;
    
            public List<Row> getR() {
                return r;
            }
    
            public ViewsHistoric() {
            }
        }
    
        public static class Row {
    
            // Triplet is an array in the original JSON
                // of three elements, but an array.
            private Triplet[] c;
    
            public Triplet[] getC() {
                return c;
            }
    
            public Row() {
            }
        }
    
        public static class Triplet {
    
            private String v;
    
            public String getV() {
                return v;
            }
    
            public Triplet() {
            }
        }
    }
    

答案 1 :(得分:1)

在对无效的实际JSON字符串进行细微更改后,只需尝试使用下面的示例代码。在这里查看JSONLint - The JSON Validator以获取JSON字符串验证。

此处结果输出为Map<String, List<Row>>,其中只有一个键r,值为Row个对象的列表。


示例代码:

Type type = new TypeToken<Map<String, List<Row>>>() {}.getType();
Map<String, List<Row>> data = new Gson().fromJson(reader, type);

System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(data));

POJO课程:

class Row {
    private List<Triplet> c;
    // getter & setter
}

class Triplet {
    private String v;
    // getter & setter
}

以漂亮的格式输出:

{
  "r": [
    {
      "c": [
        {
          "v": "03/10/2013"
        },
        {
          "v": "23"
        },
        {
          "v": "112"
        }
      ]
    },
    {
      "c": [
        {
          "v": "04/10/2013"
        },
        {
          "v": "14"
        },
        {
          "v": "232"
        }
      ]
    },
    {
      "c": [
        {
          "v": "05/10/2013"
        },
        {
          "v": "18"
        },
        {
          "v": "145"
        }
      ]
    },
    {
      "c": [
        {
          "v": "06/10/2013"
        },
        {
          "v": "22"
        },
        {
          "v": "211"
        }
      ]
    },
    {
      "c": [
        {
          "v": "07/10/2013"
        },
        {
          "v": "18"
        },
        {
          "v": "198"
        }
      ]
    },
    {
      "c": [
        {
          "v": "08/10/2013"
        },
        {
          "v": "15"
        },
        {
          "v": "215"
        }
      ]
    }
  ]
}