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);
}
}
Related
Hello My simple Json is and i have error org.json.JSONException: Index 2 out of range can you help me to solve this problem ? :
[
{
"cat_id": 593,
"title": "آلرژی و ایمونولوژی",
"sub_cat": [
{
"cat_id": 594,
"cat_title": "متخصص",
"cat_parent_fk": 593
},
{
"cat_id": 595,
"cat_title": "فوق تخصص",
"cat_parent_fk": 593
}
]
and get this json with this code but i have some error and just show two item of my json >15 item :
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
Cats cats = new Cats();
JSONObject jsonObject = (JSONObject) response.get(i);
String cat_id = jsonObject.getString("cat_id");
String title = jsonObject.getString("title");
String image_add = jsonObject.getString("image_add");
String image = jsonObject.getString("image");
JSONArray jsonArray = jsonObject.getJSONArray("sub_cat");
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject object = jsonArray.getJSONObject(i);
int sub_cat_id = object.getInt("cat_id");
String sub_cat_title = object.getString("cat_title");
int sub_parent_fk = object.getInt("cat_parent_fk");
Log.i("log", "onResponse: "+ sub_cat_id + sub_cat_title + sub_parent_fk);
}
cats.setCat_id(cat_id);
cats.setTitle(title);
cats.setImage(image);
cats.setImage_add(image_add);
list.add(cats);
catsAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
Instead of i you should use j here as
JSONObject object = jsonArray.getJSONObject(j);
// ^^
Let's assume that your response has 3 json objects and every sub_cat has 2 objects so during the parsing of 3rd object of response (when i is 2) but sublist has 2 objects (index 0,1) so know (as mentioned above) this will try to fetch 3rd object from an array(sub_cat) of 2 objects, hence the issue so use
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject object = jsonArray.getJSONObject(j);
// j represents the size of sub_cat. ^^
...
}
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 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();
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);
i am having a issue of parsing JSON data i followed this link..
over there it has parsing data images description..
and i also refereed this in stack over flow a guy who is having same issue but no correct answer..
How to Parse JSONarray inside JSONarray in android? and
How to display Image from URL?
we can say just extention of the above questions..
its not a duplicate that guy is also same problem no answer..
i have a data like below
{
"request": "ok",
"query": {
"result": [
{
"site": [
{
"latest": [
{
"id": "2eaQy8Ow",
"data": "1/1/2014"
}
]
}
],
"flag": [
"http://www.simplydecoded.com/wp-content/uploads/2013/02/Telangana2.jpg"
]
}
]
}
}
i am using below code for parsing
JSONArray json_query_flag = c.getJSONArray("flag");
JSONArray json_query_site=c.getJSONArray("site");
System.out.println("looping json_query_site");
for (int j = 0; j < c.length(); j++) {
System.out.println("looping json_query_site[" + j +"]" + "json_query_site.length() -->" + json_query_site.length());
if (j <json_query_site.length()) {
HashMap<String, String> map1 = new HashMap<String, String>();
JSONObject sd = json_query_site.getJSONObject(j);
// get latestoffers
JSONArray json_latest = sd.getJSONArray("latest");
System.out.println(json_latest.toString());
for (int k = 0; k < json_latest.length(); k++) {
HashMap<String, String> map2 = new HashMap<String, String>();
JSONObject e = json_latest.getJSONObject(k);
My problem is that i am not getting the latest and flag..
may be parsing problem..
Hellow man thanks for understanding the problem please follow this post for the till your site and latest..
it solved I am getting text.. but problem with images I think you have flag..
for that you need to change your Listviewadapter.java file. so that images will appear
change this like
String strflag = resultp.get(Mainactivity.IMAGES);
if(strflag != null)
imageLoader.DisplayImage(strflag, flag);
else
imageLoader.DisplayImage("http://www.butterentals.com/graphics/no_image.jpg", flag);
so that I will be done.
TRy this..
JSONObject JObj = new JSONObject(response);
JSONObject query = JObj.getJSONObject("query");
JSONArray result = query.getJSONArray("result");
for (int i = 0; i < result.length(); i++) {
JSONObject jobj = result.getJSONObject(i);
JSONArray site = jobj.getJSONArray("site");
for (int j = 0; j < site.length(); j++) {
JSONObject sitobj = site.getJSONObject(j);
JSONArray latest = sitobj.getJSONArray("latest");
for (int k = 0; k < latest.length(); k++) {
JSONObject lateobj = latest.getJSONObject(k);
System.out.println("id : "+lateobj.getString("id"));
}
}
JSONArray flag = jobj.getJSONArray("flag");
for (int l = 0; l < flag.length(); l++) {
System.out.println("urls : "+flag.getString(l));
}
}