7

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"

2
  • You cannot. Fetching data without object name is allowed only in array Commented Oct 8, 2014 at 10:58
  • YOu can iterate the data and get all values in that. Commented Oct 8, 2014 at 11:00

2 Answers 2

31

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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

if there is a one element, this solution worked. But didn't work for nested json elements.
can you share your JSON String?
Something like that snip2code.com/Snippet/189462/Nested-Json-Sample. Sorry about that. Cause I didn't know the json was nested.
object.getJSONObject("objectString");
Okey but, we can not sure how many nested elements are there. So object.getJSONObject("objectString"); can give us another nested JsonObject. So maybe we can write a recursive method for that. But frankly, I don't want to deal with it. Cause it's not engineering. There is must be a shortcut for that like some library's method. Thanks a lot for your interest anyway. Best Day.
|
4

This should work.

JSONObject object = new JSONObject(message);
String objectString = object.getString(object.names().get(0)); 

But it will only work if you sure that NextTransactionUrl node exists. By the way, in this case, this node could have another name, it will still work.

5 Comments

if there is a one element, this solution worked. But didn't work for nested json elements
Could you give an example ?
Something like that snip2code.com/Snippet/189462/Nested-Json-Sample. Sorry about that. Cause I didn't know the json was nested.
I see. But it could have many level before getting your attribubtes. Maybe recursivity ?
I tihnk so. But I don't understand how there is not a method for that :D Interesting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.