how to access the json array in following format - android

i am trying to access the json data from server, I have accessed the first json array from file but I don't know how to access the another json array from that file. Any help would be appreciated.
my url of json file is as follows
String jsonurl" = "http:// 66.7.207.5 /homebites/list_business_category.php?b_id=18";"
I have accessed json array "business" as follows,
json_object_main = json_parser.getJSONObjectFromUrl(jsonurl
+ res_id);
Log.i("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", json_object_main + "");
if (json_object_main != null) {
try {
JSONArray json_array_header = json_object_main
.getJSONArray("business");
JSONArray j=json_object_main.getJSONArray("business_cat");
//JSONArray json_cat=json_parser.getJsonArayFromUrl(jsonurl+res_id);
now I want to access json array "business_cat"
how can I do that?

I think you are looking for this:
JSONArray j=json_object_main.getJSONArray("business_cat");
for(int i=0; i < j.length(); i++)
{
JSONObject jsonAttribs = j.getJSONObject(i);
//do whatever you want to do with the elements
jsonAttribs.getString("SOME_KEY");
}
Hope it helps!

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.

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...
}
}

Parse JSON string from remote URL in Android

Well I'm new to Android. I'm getting a JSON string from a remote URL.
[{"key":"myString1","val":"myValue1"},{"key":"myString2","val":"myValue2"},{"key":"myString3","val":"myValue3"},{"key":"myString4","val":"myValue4"},{"key":"myString5","val":"myValue5"}]
I just need to parse this JSON string & display all key-val pair. I tried something like below from one of the tutorial.
JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(0); //This will take first pair.
But I don't know the syntax for iterating through whole json object. Any help would be appreciated. Thanks in Advance.
There's nothing special in it. You do it like iterating any other array.
Let's say you have two String arrays to be filled with values: String[] mKey, mValue
Reading from JSON array will be like:
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
mKey[i] = object.getString("key");
mValue[i] = object.getString("val");
}

Android - Json without tags - Parsing

How can I parse json from the URL? below is my json structure which does not has tags.
[{"channelId":"0465CDBE","channelName":"ATV2"},{"channelId":"06E6923B1","channelName":"Phoenix"},{"channelId":"07B4FB7ed","channelName":"N24"},{"channelId":"115B73E39","channelName":"ORF2"},
Simply get the JSONArray
JSONArray jArr = new JSONArray(jsonString);
for(int i=0;i<jArr.length;i++)
{
String jChannel = jArr.getJSONObject(i).getString("channelId");
String jChannelName = jArr.getJSONObject(i).getString("channelName");
//you can now play with these variables or add to some list or do whatever you like.
}
The outer object is a JSONArray, so you can iterate it with a "for".
Inside your json array, you have simple json objects. You can parse it by key or iterate the keys.
You can user json object and get the value with getJSONArray
examples:
testob = {"channelId":"07B4FB7ed","channelName":"N24"}, {"channelId":"115B73E39","channelName":"ORF2"},
JSONObject jsonObject = new JSONObject(testob);
JSONArray dataArray = jsonObject.getJSONArray("data");
JSONObject jsonProductData = dataArray.getJSONObject(0);

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