Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get String from JSONObject without Specific Name

Please check this code sample.

HttpEntity getResponseEntity = getResponse.getEntity();

String message = EntityUtils.toString(getResponseEntity,"UTF-8");

//message = {"EntryPointJsonResult":"{\"NextTransactionUrl\":null,\"TraceId\":null,\"IsAuthorizationRequired\":false,\"IsError\":false,\"ErrorCode\":null,\"ErrorMessage\":null}"}

JSONObject object = new JSONObject(message);
String objectString = object.getString("EntryPointJsonResult"); 
//objectString = {\"NextTransactionUrl\":null,\"TraceId\":null,\"IsAuthorizationRequired\":false,\"IsError\":false,\"ErrorCode\":null,\"ErrorMessage\":null}               

That's the question : I want to get the "objectString" without "EntryPointJsonResult". Cause this information is different at the another response.

So how can I get the "objectString" without specific key like "EntryPointJsonResult"

like image 330
Droid Avatar asked Oct 08 '14 10:10

Droid


1 Answers

You can get values of json object like this without knowing key

Iterator<String> keys= object.keys();
while (keys.hasNext()) 
{
        String keyValue = (String)keys.next();
        String valueString = object.getString(keyValue);
}
like image 154
Amy Avatar answered Sep 24 '22 15:09

Amy