Android parsing json object inside array - android

I want parse details of "name" and "id" here is my json and android code
{
"main": {
"details": [
{
"name": "name1",
"id": "id1"
},
{
"name": "name2",
"id": "id2"
}
]
}
}
and my code is:
try {
JSONObject jsono = new JSONObject(url);
SONArray jarray = jsono.getJSONArray("main");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Actors actor = new Actors();
actor.setLink(object.getString("name"));
actor.setImage(object.getString("id"));
actorsList.add(actor);
}
return true;
}
I want out put "id" and "name"

try Like this:
JSONObject totalObject = new JSONObject(result);
JSONObject mainObject = totalObject.getJSONObject("main");
JSONArray jsonArray = mainObject.getJSONArray("details");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = (JSONObject) jsonArray.get(i);
Actors actor = new Actors();
actor.setLink(object.getString("name"));
actor.setImage(object.getString("id"));
actorsList.add(actor);
}

Follow by this way:
try {
JSONObject jsono = new JSONObject(url);
SONArray jarray = jsono.getJSONArray("main");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
String name = object.getString("name");
String id = object.getString("id");
}
return true;
}

main is JSONObject, details is JSONArray, so the correct code to declare jarray is the following:
JSONArray jarray = jsono.getJSONObject("main").getJSONArray("details");

You have a JSONObject "main", containing a JSONOArray "details". This means you have to call getJSONObject("main").getJSONArray("details");
If you are going to such parsing multiple times, I'd recommend you to take a look at Gson - it is much easier with it. You have to set a SerializableName attribute to the link and image fields of the Actor class and Gson will take care to map them with the values from the json.

Related

java.lang.String cannot be converted to JSONObject. mismatch type

JSON Array
[
{
"0": {
"program_name": "Daycare"
},
"1": {
"program_name": "Preschool"
},
"program_name": [
{
"program_name": "Daycare"
},
{
"program_name": "Preschool"
}
],
"batch_name": [
{
"0": "3 Hours",
"batch_class_name": "3 Hours"
},
{
"0": "5 Hours",
"batch_class_name": "5 Hours"
}
]
}
]
This is what I've done so far: -
void getProgram() {
progressDialog = new MaterialDialog.Builder(getActivity())
.content("Please wait....")
.progress(true, 0)
.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, GlobalConfig.GET_PROGRAM_AND_BATCH_OF_TEACHER,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
progressDialog.dismiss();
Log.e("response", response);
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
JSONArray jsonProgramArray = jsonObject.getJSONArray("program_name");
for (int i = 0; i < jsonObject.length() - 1; i++) {
BatchModel programe = new BatchModel();
programe.setTitle(jsonProgramArray.getString(i));
programe.setId(jsonProgramArray.getString(i));
programlist.add(programe);
Log.e("Program test", programlist.toString());
}
} catch (JSONException e) {
e.printStackTrace();
I want to add to list strings of "program_name" which is mark in bold:
But I'm getting this error:
#user3885363 .you try this.
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
JSONArray jsonProgramArray = jsonObject.getJSONArray("program_name");
for (int i = 0; i < jsonProgramArray.length(); i++) {
JSONObject jsonObjectnew = jsonProgramArray.getJSONObject(i);
BatchModel programe = new BatchModel();
programe.setTitle(jsonObjectnew.getString("program_name"));
programlist.add(programe);
Log.e("Program test", programlist.toString());
}
you are using jsonObject.length() in your loop for (int i = 0; i < jsonObject.length() - 1; i++) but you have your program_name in the array you just get from object. JSONArray jsonProgramArray = jsonObject.getJSONArray("program_name");. Try to loop on jsonProgramArray and get the program_name object for each program.
JSONArray jsonArray = new JSONArray(response);
// Here you are getting the program JSONArray
JSONArray jsonProgramArray = jsonArray.getJSONArray("program_name");
for (int i = 0; i < jsonProgramArray.length() - 1; i++) {
// Get the each Json Object from the Array
JSONObject jsonProgramObject = jsonProgramArray.getJSONObject(i);
String program_name = jsonProgramObject.getString("program_name")
}
You can do the same thing for the other batch_name array
You have 2 "program_name" one is string inside jsonObject "0" to get that you have to do like this
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
JSONObject jsonObjt_0 = jsonObject.getJSONObject("0");
String productName = jsonObjt_0.getString("program_name")
Then you have a jsonArray named "program_name" to get that do like this.
JSONArray productArray = jsonObject.getJSONArray("program_name");
for (int i = 0; i < productArray .length(); i++) {
JSONObject listItem = productArray.getJSONObject(i);
BatchModel programe = new BatchModel();
programe.setTitle(listItem.getString("program_name"));
programe.setId(listItem.getString("program_name"));
programlist.add(programe);
}
PS. This is a weird json, consider changing the json with some meanigful names,Please don't use the same name for everything..

How to select JSON with results?

Here is my JSON file:
{
"server_response": [{
"bmw": "",
"mercedes": "",
"honda": "civic",
"toyota": "corolla",
"gmc": "",
"chevy": ""
}]
}
Here is my Android code:
try {
JSONObject jsonResponse = new JSONObject(JSON_STRING);
JSONArray jsonMainNode = jsonResponse.optJSONArray("server_response");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
//this is the problem here. How can I get JSON that has a result like Honda and Toyota?
{variable name} = jsonChildNode.optString("{problem is here}");
CarsModel carsModel new CarsModel( {variable name} {variable name} );
}
} catch (JSONException e) {
e.printStackTrace();
}
The problem is in the Android code as you can see. I want it to only get the JSON that is not empty, for example honda and toyota.
How can replace {variable name} with in this case, honda and then replace {problem is here} with the json result that's not blank?
I also want to add the {variable names} into the CarsModel carsModel new CarsModel( {variable name} {variable name} );.
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
// Get the keys in the JSON object
Iterator<?> keys = jsonChildNode.keys();
while (keys.hasNext()) {
// Get the key
String key = (String)keys.next();
String objValue = jsonChildNode.getString(key);
// check if empty
if (!objValue.isEmpty()) {
CarsModel carsModel new CarsModel(objValue);
}
}
}
This is what you need to do. The data you want is a json object inside a jsonArray
JSONObject jsonResponse = new JSONObject(JSON_STRING);
JSONArray jsonMainNode = jsonResponse.optJSONArray("server_response");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
variableX = jsonChildNode.getString("toyota");
variableY = jsonChildNode.getString("honda");
variableZ = jsonChildNode.getString("xyz");
// CarsModel thing using var x, y, z...
}

string to JSONObject conversion returns null

This is my code :
JSONObject arrayobject = new JSONObject(preferences.getString("test", ""));
String responseobject = arrayobject.getString("array"+index);
JSONObject object = new JSONObject(responseobject);
This is the arrayobject :
{"array0":"{"myarray":[{"innerkey":"innervalue"}],"key":"value"}"}
This is the responseobject (it is a string) :
{
"myarray": [
{
"innerkey": "innervalue"
}
],
"key": "value"
}
Why is object always set to NULL ??
Please help me out!
You need to take two json object for fetching json array and jsonobject from your response.
Try Below code
JSONObject jsonObject = new JSONObject(preferences.getString("test", ""));
JSONObject jsonObject1 = new JSONObject(preferences.getString("test", ""));
JSONObject jbj;
JSONArray jsonArray = new JSONArray(jsonObject .getString("myarray"));
for (int i = 0; i < jsonArray.length(); i++)
{
jbj= jsonArray.getJSONObject(i);
String innerkey= jbj.getString("innerkey");
}
String key = jsonObject1 .getString("key");
Hope it helps you.

Android json parsing object inside another object and array

I want parse all "name" and "desc" from this json object inside another json object. i need another "for"?
{
"main": {
"details": [
{
"owner_name": "owner_name1",
"id": "id1",
"details2": {
"data": [
{
"name": "name1",
"desc": "my desc1",
},
{
"name": "name2",
"desc": "my desc2",
}
],
}
},
{
"owner_name": "owner_name2",
"id": "id2",
"details2": {
"data": [
{
"name": "name3",
"desc": "my desc3",
},
{
"name": "name4",
"desc": "my desc4",
}
]
}
}
]
}
}
and My java Code is:
JSONObject jsono = new JSONObject(data);
JSONObject mainObject = jsono.getJSONObject("main");
JSONArray jsonArray = mainObject.getJSONArray("details");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object1 = jsonArray.getJSONObject(i);
JSONObject objectDetails2 = object1.getJSONObject("details2");
JSONArray jsonArrayData = objectDetails2.getJSONArray("data");
for (int j = 0; j < jsonArrayData.length(); j++) {
JSONObject object = jsonArrayData.getJSONObject(j);
Actors actor = new Actors();
actor.setName(object.getString("name"));
actor.setDesc(object.getString("desc"));
actorsList.add(actor);
}
It only show First "data" result inside details2 not showing second details2 inside data..
Now my result is: "name1,dmy desc1,name2,my desc2"
i want all result like : "name1,dmy desc1,name2,my desc2,name3,my desc3,name4,my desc4"
How can i go again inside object and array? i need details from "name"
and "desc"
Get details2 from object JSONObject and then get data JSONArray:
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
// get details2 JSONObject
if(object.has("details2")){
if(!object.isNUll("details2")){
JSONObject objectDetails2 = object.getJSONObject("details2");
// get data JSONArray from objectDetails2
if(objectDetails2.has("data")){
if(!objectDetails2.isNUll("data")){
JSONArray jsonArrayData = objectDetails2.getJSONArray("data");
// iterate to jsonArrayData
for (int j = 0; j < jsonArrayData.length(); j++) {
JSONObject objectInner = jsonArray.getJSONObject(j);
String strName=objectInner.optString("name");
....
}
}else{
// details2 found but null
}
}else{
// details2 not found
}
}else{
// details2 found but null
}
}else{
// details2 not found
}
}
I think you should go into "details2" JSON Object like this :
JSONArray jsonArrayData=object.getJSONObject("details2").getJSONArray("data");
you have to get the object first and then parse it for the diffrent names and info
JSONArray jsonArrayData;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject("details");
jsonArrayData=object.getJSONArray("data");
for(int k=0;k<jsonArrayData.length();k++)
{
Actors actor = new Actors();
actor.setName(object.getString("name"));
actor.setDesc(object.getString("desc"));
actorsList.add(actor);
}
}
i will suggest you use GSON it is quite simple to use,

JSON PARSING IN ANDROID?

I need to parse Json in Android, as a newbie in Json as well as in Android I am unable to do so here is the json String:
[
{
"chapter": "1. General",
"lessons": [
{
"lesson": "1.1 "
},
{
"lesson": "1.2"
},
{
"lesson": "1.3"
}
]
},
{
"chapter": "2.emergencies"
}
]
Here I just want to get the lessons array data. So any help will really be appreciated. Thanks
JSONObject jObject = new JSONObject(jsonString);
JSONObject chapObject = jObject.getJSONObject("chapter");
Log.d("Chapt", chapObject.getString("chapter"));
JSONArray lessonArray = popupObject.getJSONArray("lessons");
for (int i = 0; i < 3; i++) {
Log.d("Name", lessonArray.getJSONObject(i).getString("lesson").toString());
Log.d("Value", lessonArray.getJSONObject(i).getString("onclick").toString());  
}     
JSONObject chap2Object = jObject.getJSONObject("chapter");
Log.d("Chapt2", chapObject.getString("chapter"));
Use JSONObject and/or JSONArray. They can be created directly from strings, eg:
JSONObject json = new JSONObject(string);
Android includes a JSON parser: http://developer.android.com/reference/org/json/package-summary.html
String json = "{ ... }";
JSONObject obj = new JSONObject(json);
JSONArray array = obj.getJSONArray("lessions");
for (int i = 0; i < array.length(); i++) {
String lession = array.getJSONObject(i).getString("lession");
}

Categories

Resources