0

I am Parsing Dynamic JSON, so it's more complicated to Decompile using POJO (Model Classes)

Is there any Convenient way to do this, provide suggestions to me.

Here is the Issue

public class Post {

    @SerializedName("Resource")
    @Expose
    private Resource onlineResource;

    public Resource getResource() {
        return onlineResource;
    }

      public void setResource(Resource resource) {
         this.onlineResource = resource;
      }

    }


    public class Resource {

    @SerializedName(value = "_xmlns:xlink",alternate = "xmlns:xlink")
    @Expose
    private Object xmlnsXlink;
    @SerializedName(value = "_xlink:href",alternate = "xlink:href")
    @Expose
    private Object xlinkHref;

    public Object getXmlnsXlink() {
        return xmlnsXlink;
    }

    public void setXmlnsXlink(Object xmlnsXlink) {
        this.xmlnsXlink = xmlnsXlink;
    }

    public Object getXlinkHref() {
        return xlinkHref;
    }

    public void setXlinkHref(Object xlinkHref) {
        this.xlinkHref = xlinkHref;
    }

}

for Resource Class Sometimes get JSON

like :

   {
    "Resource": "abcdefgh",
   }

alternatively like :

{ 
  "Resource" :
              {
              "_xmlns:xlink":"xyz",
              "_xlink:href":"abc"
              },
}

So how could I parse this type of Random(Dynamic) Json?

Re.Thak
  • 19
  • 11
  • you can do manual parsing based on the type and then put data in model class. – Karan Khurana Jun 14 '19 at 06:35
  • i have tried but i have very large and nested JSON Response so is not convenient way to parse Dynamic Json – Re.Thak Jun 14 '19 at 06:38
  • Paste your response JSON – Karan Khurana Jun 14 '19 at 06:49
  • unable to paste whole JSON here.Some part of response : "DCPType": { "HTTP": { "Get": { "Resource": { "_xmlns:xlink": "dfds", "_xlink:href": "214df" } }, "Post": { "OnlineResource": { "_xmlns:xlink": "fdsf", "_xlink:href": "0124fs" } } } } – Re.Thak Jun 14 '19 at 07:35

3 Answers3

0

you just change the variable data type from Object To String :

private Object xmlnsXlink;   TO    private String xmlnsXlink;

And

private Object xlinkHref;  TO  private String xlinkHref;
Mitesh Machhoya
  • 404
  • 2
  • 8
  • Thanks but there is no fix response it will change just Key is fixed that's why i have used "Object" class Type over there – Re.Thak Jun 14 '19 at 06:45
  • if your key is not fixed with any predefined type then you have no any option to parse json dynamically, you must have to parse json manually. – Mitesh Machhoya Jun 14 '19 at 07:18
  • my key is fixed as xmlnslink but not fixed that will appear in JSON or not,if appear than will in JsonObject/Array or simple String. – Re.Thak Jun 14 '19 at 07:26
  • yes my mean is your key is fixed but value type is not fixed then you can not use this dynamic way. – Mitesh Machhoya Jun 14 '19 at 08:38
0

First use this class as your Resource class

    public class Resource {

    @SerializedName("_xmlns:xlink")
    @Expose
    private String xmlnsXlink;
    @SerializedName("_xlink:href")
    @Expose
    private String xlinkHref;

    public String getXmlnsXlink() {
    return xmlnsXlink;
    }

    public void setXmlnsXlink(String xmlnsXlink) {
    this.xmlnsXlink = xmlnsXlink;
    }

    public String getXlinkHref() {
    return xlinkHref;
    }

public void setXlinkHref(String xlinkHref) {
this.xlinkHref = xlinkHref;
}

}

For variable json at certain points you need to manually parse json wherever json type is variable(string/object)using this :

json instanceof JSONObject

and then you can pass it's value in a model class

Karan Khurana
  • 575
  • 8
  • 20
  • thanks but it throws exception :: expected-begin-object-but-was-string-at-line-1-column-1 which i asked this question here. and apart class is working properly with JSON having this Key value pair. as i mentioned – Re.Thak Jun 14 '19 at 06:55
  • That exception is for Resource object/string – Karan Khurana Jun 14 '19 at 06:59
  • Yeah but sometime i received Object with Key-Value pair and sometimes i received simple String value instead of Object like : { "Resource": "abcdefgh", } – Re.Thak Jun 14 '19 at 07:01
-1

It is not possible if you are using JSON parsing library such as Gson. You have to parse the JSON manually as suggested here: Parsing dynamic JSON in Android

piet.t
  • 11,718
  • 21
  • 43
  • 52
Prashant Patel
  • 1,087
  • 11
  • 18
  • Thanks for your time,It's also not helping me at all. as i mentioned i have very large Dynamic JSON response. for Now i would like ask that suppose i created Model class and get the resopnse different how can parse it – Re.Thak Jun 14 '19 at 06:43
  • This just isn't true at all; it is absolutely possible to encounter similar errors in Gson. – Ryan M Apr 13 '23 at 00:09