Json and java conversion



以下是我的Java代码的部分

Type relMapType = new TypeToken<Map<String, Map<String,   Map<String ,Map<String, List<Set<ProfileData>>>>>>>(){}.getType();
    InputStream is = new FileInputStream("D:\work.json");
    String jsonTxt;
    try {
        jsonTxt = IOUtils.toString(is);
        System.out.println(jsonTxt);
        JSONObject jsons = new JSONObject(jsonTxt); 
        String jString= gson.toJson(jsons);
            gson.fromJson(jString, relMapType);
        } catch (IOException e) {
        e.printStackTrace();
    }

以下是我的json数据。

{
   "string1":{
      "string2":{
         "string3":{
            "string4":[
               {
                  "primaryKey":{
                     "projectCode":"PC",
                     "type":"type1",
                     "code1":"data1",
                     "code2":"data2"
                  },
                  "status":"status1",
                  "Id":"123",
                  "frequency":"freq",
                  "destFre":"destFreq"
               }
            ]
         }
      }
   }
}

运行代码时,请低于异常。谁能帮我吗?

 Exception in thread "main" com.google.gson.JsonSyntaxException:    java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 5 column 21
at com.google.gson.Gson.fromJson(Gson.java:806)
at com.google.gson.Gson.fromJson(Gson.java:761)
at com.google.gson.Gson.fromJson(Gson.java:710)
at com.JsonExample.main(JsonExample.java:97)

引起的:

您在List<Set<ProfileData>>中的List中使用了Set。两者都是Collection S,因此将被视为数组。JSON的同一部分是一系列对象。删除 SetList,并且应该有效。

最新更新