0

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.

3
  • I tried one code in android post the code you tried? Commented Feb 6, 2013 at 7:03
  • use JSONArray and get their indexes can you provide your code Commented Feb 6, 2013 at 7:05
  • vogella.com/articles/AndroidJSON/article.html This would help a lot Commented Feb 6, 2013 at 7:08

2 Answers 2

3

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");
}
Sign up to request clarification or add additional context in comments.

Comments

1

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"));
}

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.