Create JSON Object with Json Array in Android - android

"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

Related

how to post nested Json Array to server?

Hi i want to post this a json array which has two json arrays of Integers in them to server using JsonObject
"job_experiences": [
{
"job_category": [Integer],
"experience_level": [Integer]
}
]
JsonArray list = new JsonArray();
for (int i = 0; i < selectedJobs.size(); i++) {
list.add(selectedJobs.get(i));
}
JsonArray list2 = new JsonArray();
for (int i = 0; i < selectedJobs.size(); i++) {
list2.add(selectedJobs.get(i));
}
JsonObject postData = new JsonObject();
postData.add("job_category", list);
postData.add("experience_level", list2);
Please use JSONObject instead of JsonObject and JSONArray instead of JsonArray,
Try below approach and check
JSONArray list = new JSONArray();
for (int i = 0; i < selectedJobs.size(); i++) {
list.put(selectedJobs.get(i));
}
JSONArray list2 = new JSONArray();
for (int i = 0; i < selectedJobs.size(); i++) {
list2.put(selectedJobs.get(i));
}
JSONObject postData = new JSONObject();
try {
postData.put("job_category", list);
postData.put("experience_level", list2);
JSONArray job_experiences = new JSONArray();
job_experiences.put(postData);
JSONObject jsonObject_job_experiences = new JSONObject();
jsonObject_job_experiences.put("job_experiences", job_experiences);
Log.e("jsonObject_job_exp", jsonObject_job_experiences.toString());
} catch (JSONException e) {
e.printStackTrace();
}
and in the log you can see the final JSONObject

Parse list inside a json in android

I am putting a list in the json and now I want to get the list and display each entry in different textview. This is my code to put the list in json and to get the same
static ArrayList<String> list = new ArrayList<String>();
JSONObject json = jsonFruitArray.getJSONObject(i);
list.add("john");
list.add("mat");
list.add("jason");
json.put("sku",new JSONArray(list));
This is how i am trying to get the list
JSONArray jsonFruitSkuArray = json.getJSONArray("sku");
for(int iSku=0; iSku< jsonFruitSkuArray.length();iSku++){
JSONObject jsonSkuObject = jsonFruitSkuArray.getJSONObject(iSku);
productBean.setSkuId1(jsonSkuObject.getInt("sku"));
}
You should try like this. Use JsonArray
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("1", 1);
jsonObject.put("2", 2);
jsonObject.put("3", 3);
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray result = new JSONArray();
result.put(jsonObject);
//Use json array
JSONObject jsonObj = new JSONObject();
JSONArray jsonArray = new JSONArray();
jsonArray.put("John");
jsonArray.put("Mat");
jsonArray.put("Jason");
jsonObj.put("json_array", jsonArray);
//Retrieve like this
JSONArray jsonFruitSkuArray = jsonObj.getJSONArray("json_array");
for(int iSku=0; iSku< jsonFruitSkuArray.length();iSku++){
JSONObject jsonSkuObject = jsonFruitSkuArray.getJSONObject(iSku);
productBean.setSkuId1(jsonSkuObject.getInt("sku"));
}
In your code you can try following :
ArrayList<String> list = new ArrayList<String>();
JSONObject json = new JSONObject();
list.add("john");
list.add("mat");
list.add("jason");
json.put("sku", new JSONArray(list));
JSONArray jsonFruitSkuArray = json.getJSONArray("sku");
for (int iSku = 0; iSku < jsonFruitSkuArray.length(); iSku++) {
String element = jsonFruitSkuArray.getString(iSku);
System.out.println(element);
}

Create json fromat in android

How can I create a in android json like this.I can create the json object but still facing some problem to make like this.
{
"IncidentDetails": {
"StreetId": "370","StartDate":"21-03-2015","EndDate":"24-03-2015",
"ImageDetails": [
{
"PhoneImageUrl": "\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150321_215458_-926186234.jpg",
"Base64ImageData": "Ioop9RLYCKTAIzRRSGIVGw9s1hvw5HvRRVRYtixan+daM2pCQ\/PH40nQ0uOOKM880guIQTmm044xnNNx",
},
{
"PhoneImageUrl": "\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150321_215458_-926186234.jpg",
"Base64ImageData": "Ioop9RLYCKTAIzRRSGIVGw9s1hvw5HvRRVRYtixan+daM2pCQ\/PH40nQ0uOOKM880guIQTmm044xnNNx",
},
{
"PhoneImageUrl": "\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150321_215458_-926186234.jpg",
"Base64ImageData": "Ioop9RLYCKTAIzRRSGIVGw9s1hvw5HvRRVRYtixan+daM2pCQ\/PH40nQ0uOOKM880guIQTmm044xnNNx",
}
]}
}
What I tried in my android code just have a look it giving totally different output
JSONArray jsonArray = new JSONArray();
JSONObject IncidentDetails = new JSONObject();
try
{
IncidentDetails.put("StartDate", EditTextStartText.getText().toString());
IncidentDetails.put("EndDate", EditTextEndText.getText().toString());
IncidentDetails.put("StreetId", StreetId);
jsonArray.put(IncidentDetails);
for(int i=0; i<length; i++)
{
JSONObject ImageDetails = new JSONObject();
ImageDetails.put("Base64ImageData", getBase64Image(ReusableClass.imgUrl.get(i)));
ImageDetails.put("PhoneImageUrl", ReusableClass.imgUrl.get(i));
jsonArray.put(ImageDetails);
}
JSONObject IncidentDetailsObj = new JSONObject();
IncidentDetailsObj.put("IncidentDetails", jsonArray);
String jsonStr = IncidentDetailsObj.toString();
My current output
{"IncidentDetails":[{"StreetId":"370","StartDate":"18-03-2015","EndDate":""},{"PhoneImageUrl":"\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150318_171451_1389057690.jpg","Base64ImageData":"\/9j\/4AAQSkZJRgABAQAAAQABAAD\/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkz\nODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P\/2wBDARESEhgVGC8aGi9jQjhCY2NjY2Nj\nY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P\/wAA"},{"PhoneImageUrl":"\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150318_171451_1389057690.jpg","Base64ImageData":"\/9j\/4AAQSkZJRgABAQAAAQABAAD\/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkz\nODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P\/2wBDARESEhgVGC8aGi9jQjhCY2NjY2Nj\nY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P\/wAA"}]}
Use this
JSONArray jsonArray = new JSONArray();
JSONObject IncidentDetails = new JSONObject();
try
{
IncidentDetails.put("StartDate", EditTextStartText.getText().toString());
IncidentDetails.put("EndDate", EditTextEndText.getText().toString());
IncidentDetails.put("StreetId", StreetId);
for(int i=0; i<length; i++)
{
JSONObject ImageDetails = new JSONObject();
ImageDetails.put("Base64ImageData", getBase64Image(ReusableClass.imgUrl.get(i)));
ImageDetails.put("PhoneImageUrl", ReusableClass.imgUrl.get(i));
jsonArray.put(ImageDetails);
}
IncidentDetails.put("ImageDetails", jsonArray);
JSONObject IncidentDetailsObj = new JSONObject();
IncidentDetailsObj.put("IncidentDetails", IncidentDetails);
String jsonStr = IncidentDetailsObj.toString();
see answer with comments,
//* IncidentalDetial is an object not an array
JSONObject IncidentDetails = new JSONObject();
//* add the first three elements
IncidentDetails.put("StartDate", EditTextStartText.getText().toString());
IncidentDetails.put("EndDate", EditTextEndText.getText().toString());
IncidentDetails.put("StreetId", StreetId);
//* contruct the imagedetails array
JSONArray jsonArray = new JSONArray();
for(int i=0; i<length; i++)
{
JSONObject ImageDetails = new JSONObject();
ImageDetails.put("Base64ImageData", getBase64Image(ReusableClass.imgUrl.get(i)));
ImageDetails.put("PhoneImageUrl", ReusableClass.imgUrl.get(i));
jsonArray.put(ImageDetails);
}
//* add the imagedetails array to the incidentDetials object
IncidentDetails.put(jsonArray);
JSONObject IncidentDetailsObj = new JSONObject();
IncidentDetailsObj .put(IncidentDetails);
String jsonStr = IncidentDetailsObj.toString();
Notice that that this is very common scenario, so there has to be some library that deals it for you :)
I recommend https://code.google.com/p/google-gson - GSON. All you have to do is to prepare POJO class that describes JSON, in your case:
public class IncidentDetails {
IncidentDetails incidentDetails;
}
public class IncidentDetails {
String streetId;
String startDate;
String endDate;
ImageDetails imageDetails;
}
Then do the same with ImageDetails. All you have to do then is to create GSON with proper naming policy (it converts automaticcaly class fields names to JSON).
gson.toJson(incidentDetails) creates String you desire. gson.fromString(String json) will create IncidentDetails object from json string.

How to create Json using JsonArray and JsonObject

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

How do I pull the string array from this json object?

I am trying to get a list of available numbers from the following json object, using the class from org.json
{
"response":true,
"state":1,
"data":
{
"CALLERID":"81101099",
"numbers":
[
"21344111","21772917",
"28511113","29274472",
"29843999","29845591",
"30870001","30870089",
"30870090","30870091"
]
}
}
My first steps were, after receiving the json object from the web service:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
Now, how do I save the string array of numbers?
use:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
JSONArray arrJson = jsonData.getJSONArray("numbers");
String[] arr = new String[arrJson.length()];
for(int i = 0; i < arrJson.length(); i++)
arr[i] = arrJson.getString(i);
you need to use JSONArray to pull data in an array
JSONObject jObj= new JSONObject(your_json_response);
JSONArray array = jObj.getJSONArray("data");
Assuming that you are trying to get it in a javascript block, Try something like this
var arrNumber = jsonData.numbers;
My code is for getting "data":
public void jsonParserArray(String json) {
String [] resultsNumbers = new String[100];
try {
JSONObject jsonObjectGetData = new JSONObject(json);
JSONObject jsonObjectGetNumbers = jsonObjectGetData.optJSONObject("results");
JSONArray jsonArray = jsonObjectGetNumbers.getJSONArray("numbers");
for (int i = 0; i < jsonArray.length(); i++) {
resultsNumbers[i] = jsonArray.getString(i);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(LOG_TAG, e.toString());
}
}

Categories

Resources