I have this data form this URL
http://api.clicky.com/api/stats/4?site_id=32020&sitekey=71bf7c1681e22468&type=visitors&output=json&json_callback=data%22
and how to get this in android,
my problem is how to access all this objects and array, help me
[
{
"type": "visitors",
"dates": [
{
"date": "2014-12-25",
"items": [
{ "value":"70" }
]
}
]
}
]
JSONArray jsonArray = new JSONArray("here is your json string ") ;
int count = jsonArray.length() ;
for (int i = 0; i < count; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i) ;
String type = jsonObject.optString("type") ;
JSONArray datesArray = jsonObject.optJSONArray("dates") ;
int datesCount = datesArray.length() ;
for(int j = 0; j< datesCount; j++){
JSONObject dateItem = datesArray.get(j) ;
String dateStr = dateItem.optString("date");
JSONArray itemsArray = dateItem.optJSONArray("items");
for(int f = 0 ; f < itemsArray.length(); f++){
JSONObject valueJson = itemsArray.get(f);
String value = valueJson.optString("value");
}
}
}
I think, you can use GSON library for parsing your JSON. Just create class for JSON and use this code:
new Gson().fromJson(json, MyGsonClass.class);
Related
I don't know how to parse a JSON if start with jsonArray instead jsonObject.
There's the JSON code.
[
{
"id": 1,
"title":
{
"rendered": "Apple apologises and fixes security flaw"
}
},
{
"id": 2,
"title":
{
"rendered": "Trophy hunting removes good genes and raises extinction risk"
}
}
...
]
I don't know how to get the JSONArray length. such as:
for (int i = 0; i < JSONArray.length(); i++)
{
JSONObject JSONObject1 = JSONArray.getJSONObject(i);
int id = JSONObject1.getInt("id");
string title = JSONObject1.getString("rendered");
}
Any help will be greatly appreciated!
Assuming that your json value is in this string variable : json
String json = "your_json_data";
JSONArray jsonarray = new JSONArray(json);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonObject = jsonarray.getJSONObject(i);
int id = jsonObject.getInt("id");
JSONObject titleObject = jsonObject.getJSONObject("title");
String rendered = titleObject.getString("rendered");
}
Try below code
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonObject= jsonarray.getJSONObject(i);
int id = jsonObject.getInt("id");
JSONObject jsonInner= jsonObject.getJSONObject("title");
string title = jsonInner.getString("rendered");
}
I have the following JSON struncture:
{
"schedule":{
"day":[
{
"id":"Monday",
"items":[
{
},
{
}
]
},
{
"id":"Tuesday",
"items":[
{
},
{
}
]
}
]
}
}
And what I basically want to do is reach the items array inside the day array which is inside the schedule object.
But whenever I try to get the second JSON array, I get getJSONArray
(int) in JSONArray cannot be applied to (java.lang.String).
JSONObject baseJsonResponse = new JSONObject(dayJSON);
JSONArray dayArray = baseJsonResponse.getJSONObject("schedule").getJSONArray("day").getJSONArray("items");
You should use Two for loop respect to JSONArray.
JSONObject obj = new JSONObject(success);
JSONObject JOBJ_Schedule = obj.getJSONObject("schedule");
JSONArray schedule_Array = JOBJ_Schedule.getJSONArray("day");
for (int i = 0; i < schedule_Array.length(); i++)
{
JSONObject jOBJ = schedule_Array.getJSONObject(i);
JSONArray jArray = jOBJ.getJSONArray("items");
for (int j = 0; j < jArray.length(); j++)
{
JSONObject jOBJNEW = jArray.getJSONObject(j);
}
}
Looks like day is a list of objects. So basically, you'd have to do something like getJSONArray("day").get(0).getJSONArray("items").
you can try this
try {
JSONObject baseJsonResponse = new JSONObject("dayJSON");
JSONObject schedule= baseJsonResponse.getJSONObject("schedule");
JSONArray day=schedule.optJSONArray("day");
for (int i=0; i<day.length(); i++) {
JSONObject data = day.getJSONObject(i);
String id = data.getString("id");
JSONArray items = data.getJSONArray("items");
for (int j = 0; j < items.length(); j++) {
JSONObject data2 = day.getJSONObject(i);
String str = data2.getString("YOurkey");
Log.e("categories", str);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
According to the JSON you provided, baseJsonResponse.getJSONObject("schedule").getJSONArray("day") will return you a JSONArray instead of JSONObject.
There is a "item" JSONArray inside each "day" JSONObject. < May be this is the reason.
You can try
for(int i = 0 ; i < baseJsonResponse.getJSONObject("schedule").getJSONArray("day").length() ; i++){
JSONArray itemArray = baseJsonResponse.getJSONObject("schedule").getJSONArray("day")
.getJSONObject(i).getJSONArray("items");
}
Please help me to parse the following JSON data given below:
{
posts: [
{
count: 1,
user_id: "1",
name: "Dave Greeneberg",
email: "daveneberg#example.com",
profile_photo: "http://phontest.lbch.com//users/user1.jpg",
contest_count: "3",
photo_count: 19,
win_count: "0",
photos: [
"images/contest/diwali1.jpg",
"images/contest/diwalc2.jpg",
"images/contest/145043cd811.png",
"images/contest/145043def03411.jpg",
"images/contest/14504ger11.jpg"
]
}
]
}
I tried the following code but values in arrayList_ph is null. I am confused about how to parse this JSON content.
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = arr.getJSONObject(0).getString("name");
user_email = arr.getJSONObject(0).getString("email");
user_profile = arr.getJSONObject(0).getString("profile_photo");
user_count = arr.getJSONObject(0).getString("count");
user_photo_count = arr.getJSONObject(0).getInt("photo_count");
contest_count = arr.getJSONObject(0).getString("contest_count");
win_count = arr.getJSONObject(0).getString("win_count");
JSONArray ph_arr= arr.getJSONObject(0).getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
arrayList_ph.add(ph_arr.getString(in));
}
}
Please help me to parse that field.
Please try this solution.
This solutions is worked for me.
JSONObject object = new JSONObject("");
JSONArray arr = object.optJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.optJSONObject(index);
user = object1.optString("name");
user_email = object1.optString("email");
user_profile = object1.optString("profile_photo");
user_count = object1.optString("count");
user_photo_count = object1.optInt("photo_count");
contest_count = object1.optString("contest_count");
win_count = object1.optString("win_count");
JSONArray ph_arr = object1.optJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.opt(in).toString();
arrayList_ph.add(str);
}
}
change your parsing code to this ,
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = object1.getString("name");
user_email = object1.getString("email");
user_profile = object1.getString("profile_photo");
user_count = object1.getString("count");
user_photo_count = object1.getInt("photo_count");
contest_count = object1.getString("contest_count");
win_count = object1.getString("win_count");
JSONArray ph_arr= object1.getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.get(in).toString();
arrayList_ph.add(str);
}
}
Try -
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts”);
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = object1.getString("name");
user_email = object1.getString("email");
user_profile = object1.getString("profile_photo");
user_count = object1.getInt("count");
user_photo_count = object1.getInt("photo_count");
contest_count = object1.getString("contest_count");
win_count = object1.getString("win_count");
JSONArray photos_arr= object1.getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.get(in).toString();
arrayList_ph.add(str);
}
}
Please cross check Following:
1) I think your json response is not properly formatted.
Validate from this: https://jsonformatter.curiousconcept.com/
Your json response should be :
{
"posts":[
{
"count":1,
"user_id":"1",
"name":"Dave Greeneberg",
"email":"daveneberg#example.com",
"profile_photo":"http://phontest.lbch.com//users/user1.jpg",
"contest_count":"3",
"photo_count":19,
"win_count":"0",
"photos":[
{
"image":"images/contest/diwali1.jpg"
},
{
"image":"images/contest/diwalc2.jpg"
},
{
"image":"images/contest/145043cd811.png"
},
{
"image":"images/contest/145043def03411.jpg"
},
{
"image":"images/contest/14504ger11.jpg"
}
]
}
]
}
2) If that still does not work...Try debugging and post your log please...
i want to parse data like below format in this format there are no any left side value for parse it so any idea how can i make it possible
JSON FORMAT
{
"labels": [
"Dec-2014",
"Jan-2015",
"Feb-2015",
"Mar-2015",
"Apr-2015",
"May-2015"
],
"data": [
0,
0,
0,
0,
0,
0
]
}
Try this
try {
JSONObject jsonObject = new JSONObject(
"{\"labels\": [\"Dec-2014\",\"Jan-2015\",\"Feb-2015\",\"Mar-2015\",\"Apr-2015\",\"May-2015\"],\"data\": [0,0,0,0,0,0]}");
JSONArray array = jsonObject.getJSONArray("labels");
for (int i = 0; i < array.length(); i++) {
String s = (String) array.get(i);
System.out.println(s);
}
JSONArray array2 = jsonObject.getJSONArray("data");
for (int i = 0; i < array2.length(); i++) {
String s = (String) array2.get(i);
System.out.println(s);
}
} catch (Exception e) {
}
Hope this will solve your problem!!!
You can get jsonData as follows.
JSONObject jsonObj=new JSONObject(urJson);
JSONArray labels=jsonObj.getJSONArray("labels");
ArrayList<String> lableList=new ArrayList<String>();
for (int i = 0; i < labels.length();i++)
{
lableList.add(labels.getString(i));
}
JSONArray data=jsonObj.getJSONArray("data");
ArrayList<String> dataList=new ArrayList<String>();
for (int i = 0; i < data.length();i++)
{
dataList.add(data.getString(i));
}
There are two array objects in your current Json, You can extract it as given below
// Data json array
JSONArray dataArry = obj.getJSONArray("genre");
ArrayList<String> data = new ArrayList<String>();
for (int j = 0; j < dataArry.length(); j++)
{
data.add((String) dataArry.get(j));
}
Same logic can be applied for labels too
Can any body tell me how I parse this type of json in android?
[
[
{
"condition_id":"1",
"condition_name":"Type 1 Diebetics"
}
],
[
{
"condition_id":"2",
"condition_name":"Type 2 dypatise"
}
]
]
Thanks
Solved
Thank you very much nayoso
Its worked for me.
String jsonString = "[
[
{
"condition_id":"1",
"condition_name":"Type 1 Diebetics"
}
],
[
{
"condition_id":"2",
"condition_name":"Type 2 dypatise"
}
]
]";
JSONArray jsonArray = new JSONArray(jsonString);
for(int i=0;i<jsonArray.length(),i++) {
JSONArray childJsonArray = jsonArray.getJSONArray(i);
JSONObject contentJsonObject = childJsonArray.getJSONObject(0);
String conditionID = contentJsonObject.getString('condition_id');
String conditionName = contentJsonObject.getString('condition_name');
Log.i("TAG","Index "+i+" condition_id "+conditionID+" condition_name "+conditionName);
}
You can do this easily with the org.json library. The whole thing is a JSONArray; at position 0 (use .get(0)) you have another JSONArray; at position 0 of that, you have a JSONObject, which maps keys to values (use .getString()).
Thanks chiastic-security also for making me understand.
You can use built in JSONArray and JSONObject classes
String jsonString = "[
[
{
"condition_id":"1",
"condition_name":"Type 1 Diebetics"
}
],
[
{
"condition_id":"2",
"condition_name":"Type 2 dypatise"
}
]
]";
JSONArray jsonArray = new JSONArray(jsonString);
for(int i=0;i<jsonArray.length(),i++) {
JSONArray childJsonArray = jsonArray.getJSONArray(i);
JSONObject contentJsonObject = childJsonArray.getJSONObject(0);
String conditionID = contentJsonObject.getString('condition_id');
String conditionName = contentJsonObject.getString('condition_name');
Log.i("TAG","Index "+i+" condition_id "+conditionID+" condition_name "+conditionName);
}
You can do this easily with the org.json library. The whole thing is a JSONArray; at position 0 (use .get(0)) you have another JSONArray; at position 0 of that, you have a JSONObject, which maps keys to values (use .getString()).
JSONArray jsonarray=new JSONArray(your_data);
for(int i=0;i<jsonarray.length();i++)
{
JSONArray array=jsonarray.getJSONArray(i);
for(int j=0;j<array.length();j++)
{
JSONObject jsonObject=array.getJSONObject(0);
String ConditionId=jsonObject.getString("condition_id");
String ConditionName=jsonObject.getString("condition_name");
}
}
JSONArray jsonArray = new JSONArray("Your Data");
JSONArray tempArray ;
net.sf.json.JSONObject tempJson ;
for(int i = 0 ; i < jsonArray.length() ; i++)
{
tempArray = jsonArray.getJSONArray(i);
tempJson = tempArray.getJSONObject(0);
tempJson.get("condition_id");
....data So On
}
try this
String data = ""; //your json data string
JSONArray jarray = new JSONArray(data);
for(int i = 0;i < jarray.length(); i++) {
JSONArray jarry1 = jarray.getJSONArray(i);
for(int j = 0; j < jarry1.length(); j++) {
JSONObject jobj = jarry1.getJSONObject(0);
String ConditionId = jobj.getString("condition_id");
String ConditionName = jobj.getString("condition_name");
}
}