android - Parsing json data with random/unknown value data type for key -


how parse json data random/unknown value type keys?in below json data,key data can json object or string depending upon response server.

response 1(json object)

 {           "responsecode": 2,           "responsemessage": "hi",           "data": {             "key": "id",             "value": "10312412",             "key2": "id2",             "key3": "id3"           }     } 

response 2(string)

   {           "responsecode": 2,           "responsemessage": "hi",           "data": "string here"     } 

the best can is

object data = response.get("data"); 

though, might able try

boolean gotobject = true; string datastring = null;  // try request jsonobject string datastring = string.valueof(response.optjsonobject("data"));  // if there wasn't jsonobject, try string if (datastring.isempty() || datastring.equals("null")) {     datastring = response.optstring("data");     gotobject = false; } 

then, can try

if (gotobject) {     jsonobject data = new jsonobject(datastring); } 

realistically, should make more consistent server api


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -