-2

I have a JSON string and I need to extract a string from it using Java (android)

The JSON string look like that :

{
    "Header": {
        "context": {
            "change": {
                "token": 3191
            },
            "_jsns": "urn:zimbra"
        }
    },
    "Body": {
        "AuthResponse": {
            "authToken": [
                {
                    "_content": "token"
                }
            ],
            "lifetime": 43199998,
            "_jsns": "urn:zimbraAdmin"
        }
    },
    "_jsns": "urn:zimbraSoap"
}

I want to get the value of _content, which is "token" is this case.

What i tried: NB: result contains the json string

//result contains the json string
JSONObject jObject = new JSONObject(result);
String aJsonString = jObject.getString("Body");
JSONObject jResult = new JSONObject(aJsonString);
String aaa = jResult.getString("authToken");

At this point I get the following error :

W/System.err: org.json.JSONException: No value for authToken

Any help will be appreciated

EDIT : Java code updated

Nab Ilovich
  • 360
  • 5
  • 14
  • 2
    Your JSON is an object. In there is a `Body` object. In the `Body` object is an `AuthResponse` object. In the `AuthResponse` object is your `authToken` array. You need to drill down the entire tree (root -> `Body` -> `AuthResponse` -> `authToken`), and you appear to be skipping a step or two. – CommonsWare Oct 24 '16 at 19:38
  • `"Body"` is an **object**, not a string. Use `getJSONObject("Body")`. From that, get another object, and then you can get the token array – OneCricketeer Oct 24 '16 at 19:39
  • @4castle : Sorry, I missed a line. – Nab Ilovich Oct 24 '16 at 19:40
  • Why the question is downgraded ? I can improve it if you give me feedbacks. – Nab Ilovich Oct 24 '16 at 19:44

1 Answers1

1

You need to traverse the JSON tree step by step

JSONObject jObject = new JSONObject(result); 
JSONObject jBody = jObject.getJSONObject("Body");
JSONObject jAuthResponse = jBody.getJSONObject("AuthResponse");
JSONArray jauthToken = jAuthResponse.getJSONArray("authToken");
JSONObject jFirst = jauthToken.getJSONObject(0);
String aJsonString = jObject.getString("_content");
Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • This is a bit bulky for extracting a single value. It could also be done as a one-liner with each step on a different line for readability. – 4castle Oct 24 '16 at 19:43
  • @Merlevede : your code works fine replacing the last line with : ` String aJsonString = jFirst.getString("_content");` – Nab Ilovich Oct 24 '16 at 19:45
  • @4castle : Can you give an exemple of a one liner or documentation about it ? – Nab Ilovich Oct 24 '16 at 19:46
  • 1
    `String token = new JSONObject(result).getJSONObject("Body").getJSONObject("AuthResponse").getJSONArray("authToken").getJSONObject(0).getString("_content");` – 4castle Oct 24 '16 at 20:01
  • @4castle It works fine, and it's more "classy". If you give an answer i'll accept it. – Nab Ilovich Oct 24 '16 at 20:09
  • Questions can't get new answers once they are closed as a duplicate. No worries though, this answer is basically the same. – 4castle Oct 24 '16 at 20:11
  • @Nab, if you know java, you could have guessed that you can chain all those lines in one single line. I did it on purpose for clarity! – Merlevede Oct 24 '16 at 20:48
  • @Merlevede Actually, I'm new to java. Dont take it bad, your answer is great. – Nab Ilovich Oct 25 '16 at 07:46