1

The format of my json object is:

   String jsonObjRecv =  {
      "response":{
       "respobj":{
        "id":<int>,
        "number":<string>,
        "validated":<boolean>
         }
        },
        "status":"ok",
        "errors":null
        }

It works when code is:

        JSONObject jsonObjCont = new JSONObject(jsonObjRecv);
        String getString= jsonObjCont.toString(2);

In this case getString != null and I can receive data, but when I try to get nested data of JSON object as like:

        JSONObject jsonObjCont = new JSONObject(jsonObjRecv);
        JSONObject regNumber = jsonObjCont.getJSONObject("respobj");
        String number= regNumber.getString("number");

it dont work.

I tried to use GSON library, but it works when:

public String parse(String jsonObjRecv) {
    JsonElement jelement = new JsonParser().parse(jsonObjRecv);
    String result = jelement.toString();
    return result;

and don't work :

public String parse(String jsonObjRecv) {
    JsonElement jelement = new JsonParser().parse(jsonObjRecv);
    JsonObject jobject = jelement.getAsJsonObject();
    jobject = jobject.getAsJsonObject("respobj");

    String result = jobject.get("number").toString();
    return result;

Where is my mistake?

4
  • When you say it does not work, what happens? Do you get an exception or unexpected output? google-gson.googlecode.com/svn/tags/1.1.1/docs/javadocs/com/… Commented Feb 3, 2013 at 22:48
  • Can you show the real JSON input? Commented Feb 3, 2013 at 22:49
  • @AnuragKapur - you just linked to an ancient version of Gson Commented Feb 3, 2013 at 22:52
  • @AnuragKapur what's the difference – the same result... Commented Feb 3, 2013 at 22:54

1 Answer 1

1

The problem is you're not accessing your JSON object correctly - it's an object that contains a response object which contains a respobj object.

Gson example follows. Note the comment in the code - you need to get the response object then get the respobj from it.

public static void main( String[] args )
{
    String jsonObjRecv =  "{\"response\":{\"respobj\":{\"id\":1,\"number\":\"22\",\"validated\":true}},\"status\":\"ok\",\"errors\":null}";

    JsonElement jelement = new JsonParser().parse(jsonObjRecv);
    JsonObject jobject = jelement.getAsJsonObject();

    // Here is where you're making an error. You need to get the outer
    // 'response' object first, then get 'respobj' from that.
    jobject = jobject.getAsJsonObject("response").getAsJsonObject("respobj");

    String result = jobject.get("number").getAsString();

    System.out.println(result);

}

Output:

22

Edit to add: Note I used getAsString() vs. toString() - if you use the latter you get the raw JSON which will incluse the quotes around the value (e.g. the output would be "22")

Sign up to request clarification or add additional context in comments.

Comments

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.