JSON Pasing - how to? - android

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

Related

why it only shows the latest data from the json array

These are my JSON data:
[
{"haberBaslik":"ekrem kimdir?"},
{"haberBaslik":"doğa kimdir?"},
{"haberBaslik":"biz kimiz?"},
{"haberBaslik":"fatih naptı?"}
]
and this code only shows the latest data, but I need to show my all data
JSONArray jsonarray = new JSONArray(s);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String Haberbaslik = jsonobject.getString("haberBaslik");
tv1.setText(Haberbaslik);
}
JSONArray jsonarray = new JSONArray(s);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String Haberbaslik = jsonobject.getString("haberBaslik");
tv1.setText(tv1.getText()+ " "+Haberbaslik);
}

getJSONArray inside another json array

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

Parsing JSON for android the JSON is valid but cant get anything back

I'm having trouble getting the String "lf" in this case North Atlantic Treaty Organization
[
{
"sf": "NATO",
"lfs": [
{
"lf": "North Atlantic Treaty Organization",
"freq": 13,
"since": 2001,
"vars": [
{
"lf": "North Atlantic Treaty Organization",
"freq": 13,
"since": 2001
}
]
}
]
}
]
//MY CODE
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray(ITEM_TAG);
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String name = c.getString("lf");
acronyms = new ArrayList<>();
acronyms.add(name);
}
} catch (Exception e) {
}
try {
JSONArray jsonArray1 = new JSONArray(JsonString);
for (int i = 0; i <= jsonArray1.length(); i++) {
JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
String mStrSf = jsonObject2.getString("sf");
Toast.makeText(getApplicationContext(),mStrSf.toString(), Toast.LENGTH_LONG).show();
JSONArray jsonArray3 = jsonObject2.getJSONArray("lfs");
for (int j = 0; j <= jsonArray3.length(); j++) {
JSONObject jsonObject4 = jsonArray3.getJSONObject(j);
String mStrIf = jsonObject4.getString("lf");
String mStrFreq = jsonObject4.getString("freq");
String mStrSince = jsonObject4.getString("since");
Toast.makeText(getApplicationContext(),mStrIf+"\n"+mStrFreq+"\n"+mStrSince, Toast.LENGTH_LONG).show();
JSONArray jsonArray5 = jsonObject4.getJSONArray("vars");
for (int k = 0; k <= jsonArray5.length(); k++) {
JSONObject jsonObject6 = jsonArray5.getJSONObject(k);
String mStrIf1 = jsonObject6.getString("lf");
String mStrFreq1 = jsonObject6.getString("freq");
String mStrSince1 = jsonObject6.getString("since");
Toast.makeText(getApplicationContext(),mStrIf1+"\n"+mStrFreq1+"\n"+mStrSince1, Toast.LENGTH_LONG).show();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
You are interpreting the json in a wrong way... if you analyse the json chain you will get this:
as you can see you have an array inside and array...
hope the picture helps you to see it better.
You have to again loop through the jsonArray to get "lf" key value.
Try this...
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray(ITEM_TAG);
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
JSONArray array = c.getJSONArray("lfs");
for(int j=0;j<array.length();j++){
JSONObject obj = array.getJSONObject(j);
String name = obj.getString("lf");
}
}
} catch (Exception e) {
}

Android: Parse the Nested JSON Array and JSON Object

I have this JSON content from a WordPress Site
"posts": [
{
"id": 67986,
"type": "post",
"title": "Launching New eBooks",
"thumbnail_images": {
"full": {
"url": "http://www.marineinsight.com/wp-content/uploads/2015/04/boiler-featured.png",
"width": 700,
"height": 500
},
"medium": {
"url": "http://www.marineinsight.com/wp-content/uploads/2015/04/boiler-featured-300x214.png",
"width": 300,
"height": 214
},
}
}
I want to fetch the url from medium to display as image. After referring to some SO questions, I have made this code and tried to go into the loop. But somehow i get the entire thumbnail_images
JSONObject jsono = new JSONObject(data);
jarray = jsono.getJSONArray("posts");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
JSONArray bigImage = object.getJSONArray("thumbnail_images");
for (int j = 0; j < bigImage.length(); j++) {
JSONObject tiObj = bigImage.getJSONObject(j);
JSONArray tiMed = tiObj.getJSONArray("medium");
for (int k = 0; k < tiMed.length(); k++) {
JSONObject tiMedU = tiMed.getJSONObject(i);
String imageURL = tiMedU.getString("url");
}
}
actor = new Actors();
actor.setName(object.getString("title"));
actor.setDescription(object.getString("url"));
actor.setImage(imageURL);
actor.setDob(object.getString("content"));
actorsList.add(actor);
}
Not able to figure out whats wrong in the loops above. Any help wld be great. Thanks
Try to use this
JSONObject jsono = new JSONObject(data);
jarray = jsono.getJSONArray("posts");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
JSONObject bigImage = object.getJSONObject("thumbnail_images");
JSONObject tiMed = bigImage.getJSONObject("medium");
String imageURL = tiMed.getString("url");
}
}
actor = new Actors();
actor.setName(object.getString("title"));
actor.setDescription(object.getString("url"));
actor.setImage(imageURL);
actor.setDob(object.getString("content"));
actorsList.add(actor);
}
jsonarray.getJSONObject(i).
getJSONObject("thumbnail_images").
getJSONObject("medi‌​um").getString("url")
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("posts");
for (int i = 0; i < jsonArray.length(); i++){
//JSONObject jsonObject1 = jsonArray.getJSONObject(i).getJSONObject("thumbnail_images");
System.out.println("apk----------------"+jsonArray.getJSONObject(i).getJSONObject("thumbnail_images").getJSONObject("medium").getString("url"));
}
response = CustomHttpClient.executeHttpGet("http://10.0.0.4:8000/login/?format=json&name="+username.getText().toString()+"&password="+pwd.getText().toString());
JSONArray jArray=new JSONArray(response);//Json Array
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);//Json Array To Json Object
Jenter code hereSONObject jsonObj2 = json_data.getJSONObject("fields");
usertype = jsonObj2.getString("usertype");
email = jsonObj2.getString("email");
}
Toast.makeText(getBaseContext(),email, Toast.LENGTH_SHORT).show();

Android: How to parse JSON Array of Array of objects

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

Categories

Resources