0

I am new to json parsing and am caught up big time. I have to parse the following:-

[
  {
    "firstname": abc,
    "lastname": xyp,
    "designation" : executive,
    "user": {
      "username": "xypabc",
      "userid": 4003,
      },
  },

  {
    "firstname": pqr,
    "lastname": vbn,
    "designation" : security,
    "user": {
      "username": "vbnpqr",
      "userid": 11231,
      },
  },    


  {
    "firstname": ghk,
    "lastname": lkj,
    "designation" : manager,
    "user": {
      "username": "lkjghk",
      "userid": 774,
      },
  }
]

I need to fetch the "login" and "userid" from above. Below is the code which I wrote :-

try {
    JSONArray jsonObj = new JSONArray(response);
    for(int i=0 ; i<jsonObj.length(); i++)
    {                                       
        JSONObject json_Data = jsonObj.getJSONObject(i);
        String userName = json_Data.getString("username");
        String userId = json_Data.getString("userid");
        Log.d("Factors","UserName :- "+userName+" ID :- "+userId);
    }
    }catch (JSONException e) {
        Log.d("Failure","Dude I have failed");
    }.

The problem is that my code ends up with exception. Please help !!!

1
  • your forgot parsing "user": { under which you ahve username and id Commented Apr 8, 2014 at 11:24

5 Answers 5

1

username and userid is within the user JSONObject parse the user JSONObject and then get the string of the username and userid.

DO like this to get the username and userid

for(int i=0 ; i<jsonObj.length(); i++)
    {                                       
        JSONObject json_Data = jsonObj.getJSONObject(i);
        String userName = json_Data.getJSONObject("user").getString("username");
        String userId = json_Data.getJSONObject("user").getString("userid");
        Log.d("Factors","UserName :- "+userName+" ID :- "+userId);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Nambi Narayanan .. works wonderfully !!! ... but I regret I cant "vote-up" ur answer as i dont have enough reputation :( . Neways thanks a ton
Consider looking into the Gson parser as well, it is quite amazing.
0

Parsing: "user"

   try {
        JSONArray jsonObj = new JSONArray(response);
        for(int i=0 ; i<jsonObj.length(); i++)
        {                                       
            JSONObject json_Data = jsonObj.getJSONObject(i);
            JSONObject user = json_Data.getJSONObject("user");
            String userName = user.getString("username");
            String userId = user.getString("userid");
            Log.d("Factors","UserName :- "+userName+" ID :- "+userId);
        }
        }catch (JSONException e) {
            Log.d("Failure","Dude I have failed");
        }

Comments

0
JSONArray jresult = new JSONArray(response);

jresult = json.getJSONArray("user");
for (int i = 0; i < jresult .length(); i++) 
{
JSONObject obj  = jresult .getJSONObject(i);
String username=obj.getString("username");
int userid=obj.getInt("userid");
}

Comments

0
if(result != null)
            {
                try
                {
                    JSONObject jobj = result.getJSONObject("result");

                    String status = jobj.getString("status");

                    if(status.equals("true"))
                    {
                        JSONArray array = jobj.getJSONArray("user");

                        for(int x = 0; x < array.length(); x++)
                        {
                            HashMap<String, String> map = new HashMap<String, String>();

                            map.put("username", array.getJSONObject(x).getString("username"));

                            map.put("userid", array.getJSONObject(x).getString("userid"));



                            list.add(map);
                        }

                        CalendarAdapter adapter = new CalendarAdapter(Calendar.this, list);

                        list_of_calendar.setAdapter(adapter);
                    }
                }
                catch (Exception e) 
                {
                    e.printStackTrace();
                }

Comments

0

Try this code : :)

  JSONArray jsonObj = new JSONArray(response);
                for(int i=0 ; i<jsonObj.length(); i++)
                {                                       
                    JSONObject json_Data = jsonObj.getJSONObject(i);
                    String firstname = json_Data.getString("firstname");
                    String lastname = json_Data.getString("lastname");
                    String designation = json_Data.getString("designation");

                    JSONArray jsons1 = json_Data.getJSONArray("user");
                    for (int j = 0; j < jsons1.length(); j++) {

                        JSONObject jsonss = jsons1.getJSONObject(j);
                         String username = jsonss.getString("username");
                            String userid = jsonss.getString("userid");
                    }

                }
                }catch (JSONException e) {
                    Log.d("Failure","Dude I have failed");
                }

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.