Hello guys I have a tricky problem which I've been solving for hours.
This is my JSON response:
{
"DATA": [
{
"Name": "Aha",
"ListData": [
{
"ID": 1
},
{
"ID": 2
},
{
"ID": 3
}
]
}
]
}
This is what I've done so far:
try {
JSONObject jsonObj = new JSONObject(result);
JSONArray jsonArray = jsonObj.getJSONArray("DATA");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject data = jsonArray.getJSONObject(i);
JSONArray arr = data.getJSONArray(Constants.LIST_DATA);
for(int j = 0; j < arr.length(); j++) {
JSONObject innerData = arr.getJSONObject(i);
int id = innerData.getInt(Constants.ID);
item = new HashMap<>();
item.put(Constants.ID, id);
itemList.add(item);
}
}
} catch(JSONException e) {
Log.e("JSONException", "" + e.toString());
}
But I'm only getting the correct size (3) of the array but I'm getting the same ID which 1. What seems to be wrong with my code? Any help would greatly appreciated. Thanks!
Result is something like this:
ID=1,ID=1,ID=1
Whereas my expected is:
ID=1,ID=2,ID=3
You should have used
JSONObject innerData = arr.getJSONObject(j);
you're doing second loop with j=0 and you're trying to getting JSONString from first loop.
Related
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 read the images and its corresponding positions
"images": [
[
{
"images_url": "http://provenlogic.info/tinder_web/public/uploads/e9275d47cf5efd929794caafc50e957982c47582.jpg",
"position": "1"
},
{
"images_url": "http://provenlogic.info/tinder_web/public/uploads/c374561da8583a77b4d21ee4b06f30d1a3fac4bb.jpg",
"position": "3"
}
]
]
try {
JSONArray jsonArray = rootObject.getJSONArray("images");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String imageUrl=jsonObject.getString("images_url");
String position=jsonObject.getString("position");
}
} catch (Exception e) {
e.printStackTrace();
}
Happy Codding!!!
A JSON Object starts with a { and ends with a } while a JSON Array starts with a [ and ends with a ].
JSONArray jArray = json.getJSONArray("images").get;
for(int i = 0; i < jArray.length;i++)
{
JSONObject jObj = jsonArray.getJSONObject(i);
String imageUrl = jObj.getString("images_url");
String position = jObj.getString("position");
}
You will need to wrap the whole JSON Handling inside a try/catch block. You might want to try http://jsonlint.com/, an online json validator and https://jsonformatter.curiousconcept.com/, an online json formatter to understand easily!
Cheers!
First of all get the JsonArray by using array name "images"
and get each Json object .
try {
JSONArray jsonArray = new JSONArray("result");
for(int i=0;i<jsonArray.length();i++){
JSONArray jsonArray1=jsonArray.getJSONArray(i);
for(int j=0;j<jsonArray1.length();j++){
JSONObject jsonObject=jsonArray1.getJSONObject(j);
String imageurl=jsonObject.getString("images_url");
String position=jsonObject.getString("position");
}
}
}catch (Exception e){
e.printStackTrace();
}
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,
I'm working on json parsing project. The error is showing as json array cant be converted into json object. Here is the json view.
{
"State Name": [
[
{
"state_name": "New South Wales (NSW)"
}
],
[
{
"state_name": "Western Australia (WA)"
}
]
]
}
java code is:
try {
JSONArray statesnames = json.getJSONArray(TAG_STATE_NAME);
Toast.makeText(getApplicationContext(), ""+statesnames.length(), 3000).show();
// looping through All Contacts
for(int i = 0; i < statesnames.length(); i++){
JSONObject c = statesnames.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_NAME);
}
If you're trying to send both arrays in one response you could do that:
[
[{"state_name":"New South Wales (NSW)"}],
[{"state_name":"Western Australia (WA)"}]
]
Take your JSONObject first as
JSONObject result= new JSONObject(json);
JSONArray arr= result.getJSONArray("State Name");
Whole JSON string is a JSONArray:
getJSONArray(0) contains JSONObjects with state_name as below:
for (int i = 0; i < arr.length(); i++) {
JSONArray firstArry = arr.getJSONArray(i);
for (int j = 0; j < firstArry.length(); j++) {
JSONObject secondarr= firstArry.getJSONObject(j);
System.out.println("state_name result: "+secondarr.getString("state_name"));
}
}
Try this..
{
"State Name":[ --> JSONArray
[ --> JSONArray
{ --> JSONObject
"state_name":"New South Wales (NSW)"
}
],
[
{
"state_name":"Western Australia (WA)"
}
]
]
}
Example :
JSONObject json = new JSONObject(response);
JSONArray statesnames = json.getJSONArray("State Name");
for (int i1 = 0; i1 < statesnames.length(); i1++) {
JSONArray jsonarray = statesnames.getJSONArray(i1);
for (int i2 = 0; i2 < jsonarray.length(); i2++) {
JSONObject jsonobject = jsonarray.getJSONObject(i2);
System.out.println("state_name : "+jsonobject.getString("state_name"));
}
}
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");
}