I want to create a Json structure which is actually a JsonArray inside a JsonObject.
The sample structure is:
1.
{
“req”: [
{
“ctrlId”:”txt1”
},
{
“ctrlId”:”txt2”
}
]
}
2.
{
“req”: [
{
“ctrlId”:”txt1”,
“val” : “val1”
},
{
“ctrlId”:”txt2”,
“val” : “val2”
}
]
}
But i am not able to get it..Any help is appreciated..
JSONObject obj = new JSONObject();
JSONArray req = new JSONArray();
JSONObject reqObj = new JSONObject()
reqObj.put( "ctrlId", "txt1" );
req.put( reqObj );
reqObj = new JSONObject();
reqObj.put( "ctrlId", "txt2" );
req.put( reqObj );
obj.put( "req", req );
The final object is obj
I have this array
{
"result": "success",
"countryCodeList":
[
{"countryCode":"00","countryName":"World Wide"},
{"countryCode":"kr","countryName":"Korea"}
]
}
Here below I am fetching country details, so I have used valArray.getJSONArray(1)
You can use valArray.getJSONArray(0)
JSONObject json = new JSONObject(jsonstring);
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);
JSONArray valArray1 = valArray.getJSONArray(1);
valArray1.toString().replace("[", "");
valArray1.toString().replace("]", "");
int len = valArray1.length();
for (int i = 0; i < valArray1.length(); i++) {
Country country = new Country();
JSONObject arr = valArray1.getJSONObject(i);
country.setCountryCode(arr.getString("countryCode"));
country.setCountryName(arr.getString("countryName"));
arrCountries.add(country);
}
you can use GSON for doing this parsing. it will make your life simple.
you can have a look at my answers in this SO question
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..
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.
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.
"ORDERDETAIL":[
{
"Description":"asdf",
"Quantity":"5",
"Unit Price":"77",
"No.":"1",
"Line No.":"100",
"Document Type":"1",
"Document No.":"109"
},
{
"Description":"torage",
"Quantity":"5",
"Unit Price":"7",
"No.":"19",
"Line No.":"10",
"Document Type":"1",
"Document No.":"10"
},
{
"Description":"IN",
"Quantity":"5",
"Unit Price":"7",
"No.":"1",
"Line No.":"1",
"Document Type":"1",
"Document No.":"9"
}
]
Code Part:
ArrayList<String> arlstKeyDetail = new ArrayList<String>();
ArrayList<String> arlstValDetail = new ArrayList<String>();
JSONArray jsonSubDataArry = new JSONArray();
JSONObject jsonSubObj = new JSONObject();
JSONObject jsonTemp1 = new JSONObject();
Iterator iterKey = jsonArrDisplayRecord.getJSONObject(1).keys();
while(iterKey.hasNext())
{
String key = (String)iterKey.next();
arlstKeyDetail.add(key);
arlstValDetail.add(jsonArrDisplayRecord.getJSONObject(1).getString(key));
}
for(int i=0 ; i < arlstKeyDetail.size() ; i++)
{
for(int j=0; j <itLength; j++){
jsonSubObj.put(arlstKeyDetail.get(j), arlstValDetail.get(j));
}
jsonSubDataArry.put(jsonSubObj);
jsonTemp1.put("ORDERDETAIL", jsonSubDataArry);
}
I want to create Json Object with Json Array like above format in android. i already tried lots of thing but didnt get as like above.
Refer to the code here. This has the same example that you are trying to achieve.
JSONObject ORDERDETAIL = new JSONObject();
JSONArray array = new JSONArray();
JSONObject array_content1 = new JSONObject();
array_content1.put("Description","asdf");
array_content1.put("Quantity","5");
.....
JSONObject array_content2 = new JSONObject();
array_content1.put("Description","torage");
array_content1.put("Quantity","5");
.....
array.add(array_content1);
array.add(array_content2);
ORDERDETAIL.put("ORDERDETAIL",array);
This is your format
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");
}