How to Extract json array within an array in android - 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.

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

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

Related

Parsing json code to a string in android using volley

i need help to parse this json code to actual strings using android volley. this is the json code:[{"name":"Tayo","0":"Tayo","thread_name":"Welcome","1":"Welcome","post":"Hi there,","2":"Hi there,","post_time":"Sunday","3":"Sunday"},{"name":"Pete","0":"Pete","thread_name":"Welcome","1":"Welcome","post":"Hi,am pete","2":"Hi,am pete","post_time":"Monday","3":"Monday"}].
I have tried other helps but not working. Thanks!
Remember:
if the .json content starts with { is considered as a Json Object.
if the .json content starts with [ is considered as a Json Array.
so you hava a JsonArray then you can parse your content like this way:
//Obtain the JsonArray
JSONArray jsonArray = new JSONArray(myJsonContent);
// Get the JsonObjects inside JsonArray
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonobject = jsonArray.getJSONObject(i);
}
// strData is the json data received.
JSONArray jsonArray = new JSONArray(strData);
for (int i=0; i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("name");
String zero = jsonObject.optString("0");
String thread_name = jsonObject.optString("thread_name");
String one = jsonObject.optString("1");
String post = jsonObject.optString("post");
String two = jsonObject.optString("2");
String post_time = jsonObject.optString("post_time");
String three = jsonObject.optString("3");
//Just an example
arrayName.add(jsonObject.optString("name"));
}
You can even use array to store data instead of the string.

Simple Json Example with android

I want to read JSON web service result.
My web service result is
[{"etkinlikTarihi":123,"eposta":"posta","etkinlikAdi":"ali","etkinlikDetay":"veli","etkinlikId":1},{"etkinlikTarihi":12,"eposta":"posta","etkinlikAdi":"ali","etkinlikDetay":"detay","etkinlikId":2},{"etkinlikTarihi":13,"eposta":"posta","etkinlikAdi":"mali","etkinlikDetay":"detay","etkinlikId":3},{"etkinlikTarihi":13,"eposta":"posta","etkinlikAdi":"mali","etkinlikDetay":"detay","etkinlikId":4}]
But, whenever I try to read with JSONObject in Android, I get a exception...
My code is here,
jsonResponse = new JSONObject(wsEtkinlikListesi);
String etkinlikTarihi = jsonResponse.getString("etkinlikTarihi");
And this is my stack trace:
org.json.JSONException: Value [{"etkinlikAdi":"toplanti","etkinlikDetay":"toplan","etkinlikTarihi":1454648400000,"etkinlikId":5,"eposta":"sefagenel#gmail.com"}] of type org.json.JSONArray cannot be converted to JSONObject
Where is my mistake and how can I correct it?
First try to parse the array and then get every object on it:
JSONArray arr = new JSONArray(jSonResultString);
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonobject = arr.getJSONObject(i);
String name = jsonobject.getString("etkinlikTarihi");
String url = jsonobject.getString("posta");
...
}

How parse json object and json array together

i have two situation of Json output .
one is data that found and i have a json array and a json object like this:
{"data":"yes"}[{"id":"10","number":"7","text":"text7","desc":"text7_again","user_code":"0"},{"id":"11","number":"8","text":"text8","desc":"text8_again","user_code":"1"}]
other situation is that data not found :
{"data":"no"}
just one json object.
how parse this data in android client for support two situtaion?
First, you should validate your json in http://jsonlint.com/ if you test it you will look that is a wrong json. So, for make it right, in your server your response should look something like this:
{"data":"yes","response":[{"id":"10","number":"7","text":"text7","desc":"text7_again","user_code":"0"},{"id":"11","number":"8","text":"text8","desc":"text8_again","user_code":"1"}]}
And in that case, in android
JSONObject jsonObj = new JSONObject(response);
if (jsonObj.getString("data").compareTo("yes") == 0) {
JSONArray jsonArray = jsonObj.getJSONArray("response");
//To-Do another code
}
and that's all
Here is a possible case: (you need to fix your json format)
Success -
string resultJSON =
{"success":true,
"data":[
{"id":"10","number":"7","text":"text7","desc":"text7_again","user_code":"0"},
{"id":"11","number":"8","text":"text8","desc":"text8_again","user_code":"1"}]}
Failed -
string resultJSON =
{"success":false}
Then
JSONObject jsonRoot = new JSONObject(resultJSON);
bool isSuccess = jsonRoot.getBoolean("success");
if (isSuccess) {
// do the array parser
for(int i=0; i<jsonData.lenght;i++) {
JSONObject jsonObj = jsonData.getJSONObject(i);
String id = jsonObj.getString("id"); // get the value of id
String desc = jsonObj.getString("desc"); // and so on...
}
}

How to parse JSON Array in android?

I have a JSON Array as follows:
jSONArray: {"li":[["apple\r\n","orange\r\n","mango\r\n"]]}
I want only the list as [apple, orange, mango] in android. Can anybody explain how to parse it in android without \r\n. I have tried as follows:
JSONArray jsonArray1 = new JSONArray(results.toString()); // which is the JSON Array
for(int i=0; i < jsonArray1.length(); i++) {
JSONObject jsonobject = new JSONObject();
JSONObject issue = jsonArray1.getJSONObject(i);
String _pubKey = issue.getString("li");
}
So, I am getting pubKey as [["apple\r\n","orange\r\n","mango\r\n"]]. I dont want \r\n.
Just replace those characters while parsing.
Example: If fruit is the string you want to put in your JSONArray jsonArray, call:
jsonArray.put(fruit.trim().replace("\r", "").replace("\n", ""));

Simple Json parsing in android

I got the Json response. I am not able to get the values from the string.my string is
Json_response is
{"NameAllList":[{"Current":{"time":"2012-02-21T08:04:21","Name":"abcd"},
"Next":{"Name":"data1","StartTime":"2012-02-21T08:06:21"}},{"Current":{"time":"2012-02-21T08:14:21","Name":"defg"},
"Next":{"Name":"data2","StartTime":"2012-02-21T08:24:21"}},{"Current":{"time":"2012-02-21T08:28:21","Name":"ghij"},
"Next":{"Name":"data3","StartTime":"2012-02-21T08:34:21"}},{"Current":{"time":"2012-02-21T08:40:21","Name":"knmo"},
"Next":{"Name":"data4","StartTime":"2012-02-21T08:48:21"}}]}
and i tried this.
JSONObject jsonObj = new JSONObject(json_response);
JSONObject subObj = jsonObj.getJSONObject("Current");
String name_current =subObj.getString("Name");
but i am not able to get the value of "Name". what mistake i have done. provide the link to do the above parsing.
first of all, your JSON response is having NameAllList as a JSON Array of objects.
So you have to fetch JSON Array first, then you can fetch one-by-one object.
for example:
JSONObject jsonString = (new JSONObject(json_response_string));
JSONArray array = jsonString.getJSONArray("NameAllList");
for(int i=0; i<array.length(); i++)
{
// Retrieve Current object as such
JSONObject objCurrent = array.getJSONObject("Current");
// Retrieve Next object as such
JSONObject objNext = array.getJSONObject("Next");
}
You are not parsing json properly, so you are not able to fetch value of Name. Please note JSON Annotation [] represent JSONArray, and {} respresent JSONObject, so method to get current item's name is:
JSONObject jsonObj = new JSONObject(json_response_string);
JSONArray jsonArr=jsonObj.getJSONArray("NameAllList");
String Hora_name_current="";
for(int i=0;i<jsonArr.length();i++)
{
JSONObject obj=jsonArr.get(i);
try{
JSONObject subObj = obj.getJSONObject("Current");
Hora_name_current =subObj.getString("Name");
break;
}catch(JSONException ex)
{
}
}
looks like you're trying to use JSONObject when you should be using JSONArray for the second request. Try this:
JSONObject jsonString = (new JSONObject(json_response_string));
JSONArray array = jsonString.getJSONArray("NameAllList");
In your JSON return, "NameAllList is actually an array and needs to be handled as such. Once you set it to "array", you can then run a for loop and treat it like any other array in Java.
Let me know if that helps.
David
JSONObject jsonObj = new JSONObject(json_response_string);
JSONArray jsonArray = jsonObj.getJSONArrays("NameAllList");

Categories

Resources