什么是无法将com.google.gson.JsonArray转换为com.google.gson.JsonObject的解决方案

时间:2016-06-18 09:56:09

标签: java

我的Json数组看起来像这样

 [
   {
      "geoRegion":"101",
      "companyName":"101",
      "department":"101",
      "industry":"101",
      "sector":"101",
      "formName":"101",
      "formType":true,
      "fields":[
         {
            "labelId":"102",
            "bizJust":"101"
         },
         {
            "labelId":"103",
            "bizJust":"101"
         },
         {
            "labelId":"104",
            "bizJust":"101"
         },
         {
            "labelId":"129",
            "bizJust":"102"
         },
         {
            "labelId":"128",
            "bizJust":"102"
         },
         {
            "labelId":"144",
            "bizJust":"102"
         },
         {
            "labelId":"143",
            "bizJust":"102"
         }
      ]
   }
]

我的代码如下:

    public Map<String,String> createTransaction(String lang) throws Exception {

            Gson gson =new Gson();
            JsonObject jsonObj=gson.fromJson(lang, JsonObject.class);//Exception thrown at this line
}

1 个答案:

答案 0 :(得分:2)

尝试将所有内容放入JSON数组而不是JSON对象。

更改您的代码
JsonObject jsonObj=gson.fromJson(lang, JsonObject.class);

为:

JsonArray array = gson.fromJson(lang, JsonArray.class);