Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Gson/inversion解析变量JSON_Java_Json_Gson_Retrofit - Fatal编程技术网

Java Gson/inversion解析变量JSON

Java Gson/inversion解析变量JSON,java,json,gson,retrofit,Java,Json,Gson,Retrofit,我正在努力将内容可变的JSON对象解析为Java对象 通常,我会尝试将JSON对象映射到POJO,但是在这种情况下,我不知道该怎么做 我的JSON如下所示: "parts": [ [ "text", "http://www.example.com/" ], [ "page", [ "http://w

我正在努力将内容可变的JSON对象解析为Java对象

通常,我会尝试将JSON对象映射到POJO,但是在这种情况下,我不知道该怎么做

我的JSON如下所示:

"parts": [
           [
             "text",
             "http://www.example.com/"
           ],
           [
             "page",
             [
               "http://www.example.com/",
               "\n\t\n\t\t\n\t\t\tSome of the Page Content preview here...",
               "",
               "/path/to/picture.jpg"
             ]
           ],
           [
             "text",
             "Another String here "
           ]
         ]
通过典型的Json-to-Java对象转换器运行这段代码不起作用,因为它无法映射到简单的POJO

我试着转换成
List>myObject但正如所料,这给了我一个例外:

W: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 9563 path $[3]./object.parts[1][1]
我想我必须为此创建一个自定义反序列化程序,但是我不知道从哪里开始

如果您能为我指明方向,我将不胜感激

编辑:正如注释中指出的,提供的JSON数据不是有效的键值对格式。我已经联系了API提供商,他们会解决这个问题


在我在前端找到解决这个问题的方法之前,我会一直回答这个问题。

第二个数组元素中的第二个元素也是您发布的JSON中的一个数组

由于GSON需要一个字符串列表,因此它无法从这一点开始解析文档

"parts": [
       ...,
       [
         "page",
         [  <--- Problematic array begin
           "http://www.example.com/",
           "\n\t\n\t\t\n\t\t\tSome of the Page Content preview here...",
           "",
           "/path/to/picture.jpg"
         ]
       ],
       ...
     ]
“部件”:[
...,
[
“第页”,

[您的JSON格式不好,我尝试将您发布的JSON检查到一个,但它无法解析您的JSON。您需要将该JSON封装在花括号中,如下所示:

{"parts": [
       [
         "text",
         "http://www.example.com/"
       ],
       [
         "page",
         [
           "http://www.example.com/",
           "\n\t\n\t\t\n\t\t\tSome of the Page Content preview here...",
           "",
           "/path/to/picture.jpg"
         ]
       ],
       [
         "text",
         "Another String here "
       ]
     ]
}
该JSON将由在线JSON格式化程序成功解析,您应该能够很好地解析它。问题是您必须处理大量的
JSON数组
,这些数组没有任何
,无法识别它们。映射它会很麻烦,因为
页面后面有
JSON数组
string

如果我建议的JSON可以映射到
List
POJO,那么您必须获取第二个元素并再次手动解析它,因为第二个数组中仍然有一个数组

希望这对你有帮助,祝你好运

问候,


里德

所以在几天后再次研究这个问题之后,我终于找到了一个适合我的案例的解决方案

与我第一次尝试将元素解析为
字符串不同,我现在将数据存储到一个简单的
java.lang.Object

我的模型现在看起来像这样:

"parts": [
           [
             "text",
             "http://www.example.com/"
           ],
           [
             "page",
             [
               "http://www.example.com/",
               "\n\t\n\t\t\n\t\t\tSome of the Page Content preview here...",
               "",
               "/path/to/picture.jpg"
             ]
           ],
           [
             "text",
             "Another String here "
           ]
         ]
@SerializedName(“部件”)
@暴露
private List parts=new ArrayList();
这可以防止GSON解析过程在检测到无效数组时使应用程序崩溃

在尝试访问数据时,我现在检查对象的类型是否为
String
,如果是这种情况,我将继续,忽略所有数组

在我的代码中,如下所示: (在本例中,我只需要parts数组中第一个元素的text属性)

List partList=myParsedObject.getParts();
如果(partList.size()>0){
if(partList.get(0.size()>1){
if(partList.get(0.get(1)instanceof String){
返回partList.get(0.get(1.toString();
}
}
}

我意识到这是一个问题,但我正在寻找检索信息的解决方法。对此有什么建议吗?您是否拥有JSON文档?此文档没有很好地利用JSON中可用的结构。不幸的是,这是我必须使用的API所提供的。它不在键值对中,您可能需要t要重新考虑json数据,您可以使用gson将其转换为LinkedHashMap/ArrayList吗?