Retrofit分析变量JSON

Retrofit分析变量JSON

本文介绍了Gson / Retrofit分析变量JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



我的JSON看起来像这样:

 parts:[
[
text,
http://www.example.com/
],
[
page,
[
http://www.example.com/,
\\\
\\ \\ t \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' /picture.jpg
]
],
[
text,
另一个字符串在这里
]
]

运行这段代码通过典型的Json to Java Object转换器不起作用,我试图转换为列表< List< String>>>>         myObject; 但正如所料,这给了我一个例外:

  W:com.google.gson。 JsonSyntaxException:java.lang.IllegalStateException:期望一个字符串,但是在第1行的BEGIN_ARRAY列9563路径$ [3] ./ object.parts [1] [1] 

我想我必须为此创建一个自定义的DeSerializer,但是我不知道从哪里开始。



任何帮助指向我的好方向都将不胜感激。

编辑:正如评论,是提供的JSON数据不是有效的键值对形成。我已经联系了API提供商,他们会解决这个问题。



直到我遇到前端处理这个问题的一种方法,我会继续打开这个问题。

解决方案

所以在几天后再次看到这个问题后,我终于找到了适用于我的案例的解决方案!



而不是试图将元素解析为 String ,这与我第一次尝试时一样。我现在将数据存储到一个简单的 java.lang.Object



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

  @SerializedName(parts)
@Expose
private List< ;列表与LT;对象>> parts = new ArrayList< List< Object>>();

这可以防止GSON分析过程在检测到无效数组时崩溃应用程序。



在尝试访问数据时,我现在检查Object是否为 String 类型,如果这种情况继续下去,忽略所有阵列。



在我的代码中,它看起来像这样:
(在我的情况下,我只需要parts数组中第一个元素的text属性)

List< List< Object>> partList = myParsedObject.getParts(); (partList.size()> 0){
if(partList.get(0).size()> 1){
if(partList.get(0))。 get(1)instanceof String){
return partList.get(0).get(1).toString();
}
}
}


I'm struggling in parsing a JSON Object with variable content to a Java Object.

Normally, I'd try to map a JSON Object to a POJO, however in this case I don't know what to do.

My JSON looks like this:

"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 "
           ]
         ]

Running this piece of code trough a typical Json to Java Object converter doesn't work because this cannot be mapped to a simple POJO.

I tried converting to List<List<String>>> myObject; but as expected this gives me an exception:

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]

I think I'll have to create a custom DeSerializer for this, however I have no idea where to start.

Any help pointing me in the good direction would be greatly appreciated.

EDIT: As pointed out in the comments, is the JSON data supplied not in valid key-value pair formation. I've contacted the API providers and they will sort this out.

Until I come across a way of dealing with this problem on the frontend, I'll keep this question open.

解决方案

So after looking at this problem again after af few days, I've finally found a solution that works for my case!

In stead of trying to parse the element into a String as in my first attempts. I'm now storing the data to a simple java.lang.Object

My model now looks like this:

@SerializedName("parts")
@Expose
private List<List<Object>> parts = new ArrayList<List<Object>>();

This prevents the GSON parsing process from crashing the app when the invalid array is detected.

When trying to access the data, I now check if the Object is of type String, if this is the case I continue, ignoring all Arrays.

In my code this looks like this:(In my case, I only need the text attribute of the first element in the parts array)

 List<List<Object>> partList = myParsedObject.getParts();
    if (partList.size() > 0) {
        if (partList.get(0).size() > 1) {
            if (partList.get(0).get(1) instanceof String) {
                return partList.get(0).get(1).toString();
            }
        }
    }

这篇关于Gson / Retrofit分析变量JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:48