0

So I am receiving a JSON array in an httpresponse. The array consists of the following objects/variables.

{"sessid":"vxkEXkMBUmBByESRlvaxrxSaFTfhDqd8","session_name":"SESS88cdfb2f1c420898","user":{"uid":"60","name":"abc","theme":"","signature":"","signature_format":"filtered_html","created":"13082976","access":"1386287","login":1386211,"status":"1","timezone":null,"language":"","picture":null,"data":{"mimemail_textonly":0},"roles":{"2":"authenticated user","5":"centre user"},"field_centre_reference":{"und":[{"nid":"256"}]},"field_first_name":{"und":[{"value":"web","format":null,"safe_value":"web"}]},"field_surname":{"und":[{"value":"services","format":null,"safe_value":"services"}]},"bounce_mail_blocked":false,"force_password_change":"0"}}

Now I want to receive all these objects/strings in separate variables. Like i want to store the "sessid" in a variable String session_id. And so on. I can get the first two (i.e. sessid and session_name) in a simple way with the help of the following code.

response = client.execute(httppost);

BasicResponseHandler handler = new BasicResponseHandler();
String data = handler.handleResponse(response);
jObj = new JSONObject(data);

sessid = jObj.getString("sessid");
Log.d("sessid obj", sessid);
session_name = jObj.getString("session_name");
Log.d("session_name", session_name);

But since I am a noob at Android, I don't know how to get the rest of the data to be saved in variables. The upcoming data cannot be saved in a simple way.

5 Answers 5

3

Try with this:

 JSONObject j_user = jObj.getJSONObject("user");
 String uid = j_user.getString("uid");
 ...
 //And so on with the rest of the fields
Sign up to request clarification or add additional context in comments.

Comments

3
{ // json object node 
    "sessid": "vxkEXkMBUmBByESRlvaxrxSaFTfhDqd8",
    "session_name": "SESS88cdfb2f1c420898",
    "user": { // json object user 
        "uid": "60", // string 
        "name": "abc",
        "theme": "",
        "signature": "",
        "signature_format": "filtered_html",
        "created": "13082976",
        "access": "1386287",
        "login": 1386211,
        "status": "1",
        "timezone": null,
        "language": "",
        "picture": null,
        "data": { // json object data
            "mimemail_textonly": 0 //string
        },
        ....// rest of the json

{ represents json object node

[ represents json array node

To parse

 JSONObject jObj = new JSONObject(load());
 String sessid = jObj.getString("sessid");
 Log.d("sessid obj", sessid);
 JSONObject user = jObj.getJSONObject("user");
 String uid = user.getString("uid"); 
 Log.d("sessid obj", uid);

To parse data

 "data": {
        "mimemail_textonly": 0
    },


   JSONObject data= user.getJSONObject("data");
   Log.i(".......",""+data.getString("mimemail_textonly"));

To parser field_centre_reference

 "field_centre_reference": { // json object field_centre_reference
            "und": [  // json array und
                {        // json  object node 
                    "nid": "256" //string
                }
            ]
        },

  JSONObject field= user.getJSONObject("field_centre_reference");
  JSONArray jr = field.getJSONArray("und");
  JSONObject jb1 = (JSONObject) jr.get(0);
  Log.i(".......",""+jb1.getString("nid"));

Comments

1

Check out JacksonParser library, it provides annotations which make your work so easy. And it is one of fastest parsing libraries... You can have it here

Edit: As an example you can take a look at one of my previous question JacksonParser databind and core cause "Found duplicate file for APK"?

Comments

0

You should notice that the JSON you received is a nested JSON. It is like a Map that one of the value of Map is also a Map. In your case, there is a JSONObject contains sessid and session_name while is only string, but the "user" is also a JSONObject, so you can parse it level by level. For more details, you can read http://www.vogella.com/articles/AndroidJSON/article.html

Comments

0

Try this..

Getting

 "user": {
        "uid": "60",

JSONObject obj_user = jObj.getJSONObject("user");
String uid = obj_user.getString("uid");

 "user": {
            "uid": "60",
"data": {
            "mimemail_textonly": 0
        }

JSONObject obj_data = jObj.getJSONObject("data");
String mimemail_textonly = obj_user.getString("mimemail_textonly");
JSONObject obj_roles = jObj.getJSONObject("roles");
String two = obj_roles.getString("2");

"field_centre_reference": {
            "und": [
                {
                    "nid": "256"           

JSONObject obj_field_centre_reference = jObj.getJSONObject("field_centre_reference");
JSONArray ary_und = obj_field_centre_reference.getJSONArray("und");
JSONObject objve = ary_und.getJSONObject(0);
String nid= objve.getString("nid");

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.