2

i am getting json array in the output.i want to access the specific key elments from the response .how can i ..?

 ResponseEntity <String> respone;
      try {
          response =
      restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);



      String response=response.getBody(); 
      JSONObject res = new JSONObject();
      res.put("result", response);
      System.out.println(res);
      int len=res.size();
      System.out.println(len);
      JSONParser parser=new JSONParser();
        Object obj = parser.parse(response);
        JSONArray array = (JSONArray)obj;
        System.out.println(array.get(0)); } 

this is respponse format i m getting in output.i want to access the bid from the response.how can i?

  [
      {
            "bName": "abc", 
            "bId": "n86nbnhbnghgy76"

          }
        ]
4
  • 1
    jackson? gson? libraries that help you parse pretty easily through elements Commented Sep 29, 2014 at 8:05
  • @sfat ... i am using jackson.. Commented Sep 29, 2014 at 8:08
  • you're using JSONParser. If you have jackson in your project, just do a ObjectMapper mapper = new ObjectMapper(); AClassThatMatchesTheModelOfThatJson thatModel = mapper.readValue(response, AClassThatMatchesTheModelOfThatJson.class); Commented Sep 29, 2014 at 8:12
  • 1
    possible duplicate of Accessing members of items in a JSONArray with Java Commented Sep 29, 2014 at 8:12

2 Answers 2

2

Decode your string using JSONArray(String json) constructor:

String response = response.getBody(); 
JSONArray res = new JSONArray(response);
String bId = res.getJSONObject(0).get("bId");
System.out.println(bid);
Sign up to request clarification or add additional context in comments.

3 Comments

it showing me error like this - The constructor JSONArray(String) is undefined for this line JSONArray res = new JSONArray(response);
...i am using google json library.how can i slove the above error?
I don't use this implementation.
0

EDIT

Try following:

  String response=response.getBody(); 
  JSONObject res = new JSONObject();
  System.out.println(res);
  int len=res.size();
  System.out.println(len);
  JSONParser parser=new JSONParser();
    Object obj = parser.parse(response);
    JSONArray array = (JSONArray)obj;
    res=(JSONObject)array.get(0);
    System.out.println(res.get("bId"));

Output :

n86nbnhbnghgy76

This one is based on your code and with Simple Json Library.

6 Comments

.still same error - The constructor JSONArray(String) is undefined
Note JSONArray.fromObject(response); is what youshould use look at second line.
..it showing this error- The method fromObject(String) is undefined for the type JSONArray
Download json library from sourceforge.net/projects/json-lib link. You'll be good to go.
i have added import org.json.simple.JSONArray; import org.json.simple.JSONObject;this class.but still same error.
|

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.