0

MY JSON response body from a service as follows

 {
 "Employee": {
 "Name": "Demo",
 "applied": true
 }
 }

I want to parse using JSON Object in Java. i did like this

JSONObject obj = new JSONObject(String.valueOf(responseBody));
//responbosy is a JSONObject type 
obj.getString("Employee[0].name");

Please suggest how to do that

asked Mar 18, 2015 at 9:19
2
  • And what is wrong ?? Commented Mar 18, 2015 at 9:45
  • It's not giving result throwing Exception Commented Mar 18, 2015 at 9:46

2 Answers 2

2

Employee is not an array, only JSONObject So you have do something like that:

obj.getJSONObject("Employee").getString("Name");
answered Mar 18, 2015 at 9:46
Sign up to request clarification or add additional context in comments.

Comments

1

I Think you want to have the name, yes?

Anyway, you can access it by using:

 JSONObject obj = new JSONObject(String.valueOf(responseBody));
 JSONObject employee = new JSONObject(obj.getJSONObject("Employee"));
 employee.getString("Name"); 
 employee.getBoolean("applied");

Reason for this is:

Everything between

 {} 

is an JSONObject. Everything between

[]

means it's an JSONArray.

In your String

 {
"Employee": {
 "Name": "Demo",
 "applied": true
 }
 }

You've an JSONObject because of starting with {}. Within this JSONObject you have an Propertie called "Employee" which has another JSONObject nested.

Be Carefull: applied is from type boolean, since it's true/false without "". If there's a number you should get it using getInteger(). if it's a boolean you can get it using getBoolean() and elsehow you should get it using getString().

you can see all available Datatypes at http://en.wikipedia.org/wiki/JSON

answered Mar 18, 2015 at 9:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.