Parsing JSONArray In Android
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': {
Solution 1:
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");
}
Solution 2:
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);
}
Solution 3:
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");
}
Solution 4:
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");
}
Solution 5:
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();
}
Post a Comment for "Parsing JSONArray In Android"