android org.json.JSONArray cannot be converted to JSONObject? - android

[{"Episode Detail":[{"Episode-image":"http:\/\/app.lionforge.com\/comics\/adminpanel\/episode_image\/014929Quincredible_1-6.png","Episode-description":" dsdsdsdsds ","Episode-video":"http:\/\/www.youtube.com\/embed\/KhXTLAdadlw"},{"Episode-image":"http:\/\/app.lionforge.com\/comics\/adminpanel\/episode_image\/015041Quincredible_1-5.png","Episode-description":" avbcabc ","Episode-video":"http:\/\/www.youtube.com\/embed\/5ORBHSJhXew"}]}]
JSONObject jObject=null;
try {
jObject = new JSONObject(response);
JSONArray list = jObject.getJSONArray("Episode Detail");
for (int i = 0; i < list.length(); i++) {
JSONObject element = list.getJSONObject(i);
episodebean bean=new episodebean();
i am getting json exception that JSONArray cannot be converted to JSONObject,how i resolve this ,i am so confuse..,please help me..

use following code
JSONObject jObject=null;
try {
JSONArray array = new JsonArray(response)
jObject = jsonArray.getJSONObject(0);
JSONArray list = jObject.getJSONArray("Episode Detail");
for (int i = 0; i < list.length(); i++) {
JSONObject element = list.getJSONObject(i);
episodebean bean=new episodebean();
your have one jsonArray that has one JsonObject

Change this
jObject = new JSONObject(response);
To
JSONArray jarray = new JSONArray(response);
JSON
[ //json array node
{ // json object node
"Episode Detail": [ // json array episode detail
{ // json obect node j
"Episode-image": "http://app.lionforge.com/comics/adminpanel/episode_image/014929Quincredible_1-6.png",
"Episode-description": " dsdsdsdsds ",
"Episode-video": "http://www.youtube.com/embed/KhXTLAdadlw"
},
{
"Episode-image": "http://app.lionforge.com/comics/adminpanel/episode_image/015041Quincredible_1-5.png",
"Episode-description": " avbcabc ",
"Episode-video": "http://www.youtube.com/embed/5ORBHSJhXew"
}
]
}
]

JSONArray arr = new JSONArray("");
for (int i = 0; i < arr.length(); i++) {
JSONObject c = json_data.getJSONObject(i);
JSONArray arrdata = c.getJSONArray("Episode Detail");
}

The root of your JSON is an JSONArray

response is Array not object.
jObject = new JSONObject(response);
it should be Array instead of object

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

Android Studio: of type org.json.JSONObject cannot be converted to JSONArray

I get error: of type org.json.JSONObject cannot be converted to JSONArray when I try to retrieve JSON values.
JSONArray jsonarray = new JSONArray(data);
for(int i = 0; i < jsonarray.length(); i++){
JSONObject JO = (JSONObject) jsonarray.get(i);
String id = JO.getString("languages");
dataParsed = dataParsed + id;
}
The JSON looks like this (have removed sensitive data)
{
"ip":"",
"type":"",
"continent_code":"EU",
"continent_name":"Europe",
"country_code":"GB",
"country_name":"United Kingdom",
"region_code":"ENG",
"region_name":"England",
"city":"",
"zip":"",
"latitude":,
"longitude":,
"location":{
"geoname_id":,
"capital":"London",
"languages":[
{
"code":"en",
"name":"English",
"native":"English"
}
],
"country_flag":"http:\/\/assets.ipstack.com\/flags\/gb.svg",
"country_flag_emoji":"\ud83c\uddec\ud83c\udde7",
"country_flag_emoji_unicode":"U+1F1EC U+1F1E7",
"calling_code":"44",
"is_eu":true
}
}
I should mention that the data I actually want to retrieve is the name of the language.
You could try this:
JSONObject obj = new JSONObject(data);
JSONObject objLocation = obj.getJSONObject("location");
JSONArray arrayLanguage = objLocation.getJSONArray("languages");
for (int i = 0; i < arrayLanguage.length(); i++) {
JSONObject JO = (JSONObject) arrayLanguage.get(i);
String id = JO.getString("languages");
dataParsed = dataParsed + id;
}

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

at 0 of type org.json.JSONArray cannot be converted to JSONObject

I have values stored in the JSON url and parsing the value from json then displaying in the app.I'm using Async task to get the values from JSONArray and display in the list. when i get the values from server i got error and the json values are
[{"name":"John","uuid":"B9407F30-F5F8-466E-AFF9-25556B57FE6D","major_id":"67889","minor_id":"7032","notification":"Welcome","type":"Website","website":"estimote.com"}]
Log:
10-16 17:46:38.996: W/System.err(11224): org.json.JSONException: Value [{"notification":"Welcome","uuid":"B9407F30-F5F8-466E-AFF9-25556B57FE6D","type":"Website","website":"estimote.com","major_id":"67889","minor_id":"7032","name":"John"}] at 0 of type org.json.JSONArray cannot be converted to JSONObject
10-16 17:46:38.996: W/System.err(11224): at org.json.JSON.typeMismatch(JSON.java:100)
10-16 17:46:38.996: W/System.err(11224): at org.json.JSONArray.getJSONObject(JSONArray.java:484)
code:
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
//JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray jarray = new JSONArray(jsonStr);
jObject = jarray.getJSONObject(0);
Log.d("jsonObj_Response: ", "> " + jarray);
// Getting JSON Array node
contacts = jObject.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
//TAG_ID, TAG_NAME, TAG_major_id, TAG_minor_id, TAG_notification, TAG_type
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_major_id);
String address = c.getString(TAG_minor_id);
String gender = c.getString(TAG_notification);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_major_id, email);
contact.put(TAG_minor_id, address);
// adding contact to contact list
contactList.add(contact);
}
you missed a JSONArray
JSONArray jarray = new JSONArray(jsonStr);
for (int i = 0; i < jarray.length(); i++) {
JSONArray innerArray = jarray.optJSONArray(i);
for (int j = 0; j < innerArray.length(); j++) {
jObject = innerArray.getJSONObject(j);
}
}
After OP's Edit
JSONArray jarray = new JSONArray(jsonStr);
for (int i = 0; i < jarray.length(); i++) {
JSONObject jobj = jarray.optJSONObject(i);
if (jobj != null) {
}
}
jObject = jarray.getJSONObject(0) is a JSONArray, your string has an object within an array within an array ;)
You have two arrays ("[["/"]]") so "jObject = jarray.getJSONObject(0);" fails as it tries to convert "[{"name" ... estimote.com"}]" to a JSONObject but this is a JSONArray.
jarray[0] is a JSONarray not a JSONObject.
My soluation as follows ,
JSONArray arr = new JSONArray(jsonStr);//Response string is here
for (int i = 0; i < arr.length(); i++) {
JSONArray arr2 = arr.optJSONArray(i);
for (int j = 0; j < arr2.length(); j++) {
jObject = arr2.getJSONObject(0);
}
}

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