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("medium").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();
Related
This is my JSON (full JSON is here : https://pastebin.com/kyPMWcTT):
"replies": [
[
{
"id": 2,
"parent": 0,
"author": 1,
"author_name": "admin",
"author_url": "http://localhost/wordpress",
"date": "2021-05-02T08:38:00",
"content": {
"rendered": "<p>Nice Blog, Awesome !</p>\n"
},
"link": "http://localhost/wordpress/2021/05/01/one-pot-thai-style-rice-noodles/#comment-2",
"type": "comment",
}
]
I am trying to get the rendered item which is inside the content Object .
This is the Code i have tried :
JSONArray replyArray = embeddedObject.getJSONArray("replies");
for (int j = 0; j < replyArray.length(); j++) {
JSONObject contentObject = replyArray.getJSONObject(j);
JSONObject getContent = contentObject.getJSONObject("content");
String reply = getContent.getString("rendered");
Log.e("Reply is", reply);
}
But, the Logcat output is :
Value at 0 of type org.json.JSONArray cannot be converted to JSONObject
How to solve this problem ? What am i doing wrong ? Please Guide
What's Happening?
We are trying to fetch the JSONObject immediately after replies JSONArray. But, the actual content lies in the following hierarchy.
replies JSONObject -> JSONArray -> JSONArray -> ContentObject -> Content
Solution
Replace this
JSONArray replyArray = embeddedObject.getJSONArray("replies");
for (int j = 0; j < replyArray.length(); j++) {
JSONObject contentObject = replyArray.getJSONObject(j);
JSONObject getContent = contentObject.getJSONObject("content");
String reply = getContent.getString("rendered");
Log.e("Reply is", reply);
}
With this
JSONArray replyArray = embeddedObject.getJSONArray("replies");
for (int j = 0; j < replyArray.length(); j++) {
JSONArray replySubArray = replyArray.getJSONArray(j);
for (int i = 0; i < replySubArray.length(); j++) {
JSONObject contentObject = replySubArray.getJSONObject(i);
JSONObject getContent = contentObject.getJSONObject("content");
String reply = getContent.getString("rendered");
Log.e("Reply is", reply);
}
}
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);
}
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");
}
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) {
}