简体   繁体   English

GSON我可以只转换JSON的一部分吗,我想使用GSON.fromJSON()

[英]GSON Can I convert only part of JSON, the ones I want Using GSON.fromJSON()

I am working with a big JSON object which has responses form multiple requests. 我正在使用一个大型JSON对象,该对象具有来自多个请求的响应。

And the part I am working on requires only few object and they are not always in front. 而我正在研究的部分只需要很少的对象,而它们并不总是在前面。 For Example the json structure is: 例如,json结构为:

** **

json = {
    mainDocument: {
        element1: {
            element11: "value11",
            element12: {
                element121: "value121"
                }
            },
        element2: {
            element21: {
                element211: {
                    element2111: "value2111",
                    element2112: {
                        element21121: "value21121"
                        }
                    }
                },
            element22: "value22"
            }
        }
    }

** **

This structure can change depending on whether or not the request is successful. 该结构可以根据请求是否成功而改变。

Now, I want to create an java object with the value of element11, element 22, element21121. 现在,我想创建一个元素为element11,元素22,element21121的java对象。

Currently I just check the json and use the setters of the object. 目前,我只检查json并使用对象的设置器。

I want to know if there is a way to let GSON handle this and not have to parse the json myself. 我想知道是否有一种方法可以让GSON处理此问题,而不必自己解析json。

Thanks in advance for any help you can offer. 在此先感谢您提供的任何帮助。

I don't know if I understand your question very well, but in order to deserialize a JSON response with Gson, the most proper way in my opinion is to create a class structure that encapsulates the data in the response. 我不知道我是否很好地理解了您的问题,但是为了用Gson反序列化JSON响应,我认为最合适的方法是创建一个将数据封装在响应中的类结构。 In your case something like: 在您的情况下,例如:

class Response
  MainDocument mainDocument

class MainDocument
  Element element1
  Element element2

class Element
  ...

If you only need some data from the JSON, you can omit attributes in your class structure and Gson will ignore them. 如果只需要JSON中的某些数据,则可以在类结构中省略属性,Gson将忽略它们。 And if an object can have different contents in different responses, you can have something like this: 而且,如果一个对象在不同的​​响应中可以具有不同的内容,则可以具有以下内容:

class Response
  MainDocument mainDocument
  Error error

And Gson will parse responses both with a root element mainDocument (like the one in the question) or with a root element error ... this allows you to adapt your parsing to variable responses... 而且Gson会使用根元素mainDocument (例如问题中的根元素)或根元素error来解析响应...这使您可以将解析适应变量响应...

Obviously, to follow this approach, you need to know all the possible response structures you can have. 显然,要遵循这种方法,您需要了解所有可能的响应结构。 If your problem is that your JSON response is absolutely variable, and you cannot create a class struture to wrap it, you always could do a manual parsing, somehting like this: 如果您的问题是JSON响应是绝对可变的,并且您无法创建用于包装它的类结构,则始终可以进行手动解析,如下所示:

JsonParser parser = new JsonParser();
JsonObject rootObj = parser.parse(jsonString).getAsJsonObject();    
String element21121 = rootObj
                       .getAsJsonObject("mainDocument")
                       .getAsJsonObject("element2")
                       .getAsJsonObject("element21")
                       .getAsJsonObject("element211")
                       .getAsJsonObject("element2112")
                       .getAsString("element21121");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM