Sibin Francis
Sibin Francis

Reputation: 601

How to Extract json array within an array in android

I am trying to pass some json encoded data from cakephp to android, I have a problem when passing the data

I use
echo json_encode($todaysdata); exit();

in CakePhp and when I debug this code in cakephp, I get a result in the browser

[{"status":{"uname":"sibin","pass":"shanu","upid":14}},
    {"status": {"uname":"amal","pass":"amalu","upid":14}}
]

I need to extract these two status details seperately in Android

I tried one code in android, it gives result, but result is repeated I want the result of two status seperately If anybody know, please help me.

Upvotes: 0

Views: 814

Answers (2)

Chirag
Chirag

Reputation: 56925

Try with the following code.

JSONArray jsonArray = new JSONArray("yourJsonResponseInString");

for(int i=0;i<jsonArray.length();i++)
{
    JSONObject e = jsonArray.getJSONObject(i);
    JSONObject jsonObject = e.getJSONObject("status");
    Log.i("=== UserName","::"+jsonObject.getString("uname"));
    Log.i("=== Password","::"+jsonObject.getString("pass"));
    Log.i("=== UId","::"+jsonObject.getString("upid"));
}

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

set status value as from current json String :

JSONArray jsonarr = new JSONArray("your json String");
for(int i = 0; i < jsonarr.length(); i++){

  JSONObject jsonobj = jsonarr.getJSONObject(i);
   // get status JSONObject
  JSONObject jsonobjstatus = jsonobj.getJSONObject("status");
  // get uname
  String str_uname=jsonobjstatus.getString("uname");
  // get pass
  String str_pass=jsonobjstatus.getString("pass");
  // get upid
  String str_upid=jsonobjstatus.getString("upid");
}

Upvotes: 3

Related Questions