how to post nested Json Array to server? - android

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

Related

How to get some string from JsonObject?

I got from server response by retrofit, that is actually JsonObject(using Gson):
{"a": "a and its content 1", "b": [{"b_1": "string: b_1", "b_2": 2222}]}
so that I get it like this:
JsonObject jsonObject = response.body();
And then I can log it:
Log.d(TAG, jsonObject.get("a")+"");
// log: "a and its content 1"
question:
How can I log only "string: b_1"? (from "b_1": "string: b_1")
As it is in array [], hard to get it for me.
Try this one work for me,
try {
JSONObject jsonObject = response.body();
JSONArray jsonArray = jsonObject.getJSONArray("b");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
String b_1 = jsonObj.getString("b_1");
int b_2 = jsonObj.getInt("b_2");
}
} catch (JSONException e) {
e.printStackTrace();
}
try this
try{
JsonObject jsonObject = response.body();
JSONArray a = jsonObject.getJSONArray("b")
for (int i = 0; i < a.length(); i++) {
Log.d("Type", a.getString(i));
}
}catch(Exception e){
}
Try like below
JSONObject jObj = response.body();
JSONArray jsonArray = jObj.getJSONArray("b");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.d(TAG, jsonObject.getString("b_1")+"");
Log.d(TAG, jsonObject.getInt("b_2")+"");
}
try this
JSONObject jsonObject = new JSONObject(responseString);
String status = String.valueOf(jsonObject.get("a"));

array of objects

For post method in android
I have to send array of objects.
I am retrieving elements from an arrayList and putting that retrieved element in JSONObject and than putting that in JSONobject in JSONArray.
JSONArray ccArray = new JSONArray();
{
JSONObject object = new JSONObject();
if (ccArrayList.size() != 0) {
for (int i = 0; i < ccArrayList.size(); i++) {
object.put("emailId", ccArrayList.get(i));
ccArray.put(object);
}
} else {
object.put("", "");
}
}
When there are more than 2 or more than 2 elements in arraylist it is adding the last element in ccrray as many times as their are elements in the list.
output :
"cc":[{"emailId":"f#j.com"},{"emailId":"f#j.com"}]
Change code like this
JSONObject obj = new JSONObject();
JSONArray ccArray = new JSONArray();
for (int i = 0; i < ccArrayList.size(); i++) {
JSONObject object = new JSONObject();
if (ccArrayList.size() != 0) {
object.put("emailId", ccArrayList.get(i));
ccArray.put(object);
} else {
object.put("", "");
}
}
obj.put("cc",ccArray);
Code:-
ArrayList<String> ccArrayList = new ArrayList<String>();
ccArrayList.add("abc#xyz.com");
ccArrayList.add("abc#xyz.com");
ccArrayList.add("abc#xyz.com");
ccArrayList.add("abc#xyz.com");
ccArrayList.add("abc#xyz.com");
JSONArray ccArray = new JSONArray();
if(ccArrayList.size()>0){
for(int i=0;i<ccArrayList.size();i++){
JSONObject object = new JSONObject();
try {
object.put("emailId", ccArrayList.get(i));
} catch (JSONException e) {
e.printStackTrace();
}
ccArray.put(object);
}
}
Output:-
ccArray: [{"emailId":"abc#xyz.com"},{"emailId":"abc#xyz.com"},{"emailId":"abc#xyz.com"},{"emailId":"abc#xyz.com"},{"emailId":"abc#xyz.com"}]
That's because you declared JSONObject object = new JSONObject(); outside the for loop, so each time it loops the object adds a new JSONObject and also does the array. Try keeping it within the loop:
JSONArray ccArray = new JSONArray();
for (int i = 0; i < ccArrayList.size(); i++) {
JSONObject object = new JSONObject();
object.put("emailId", ccArrayList.get(i));
ccArray.put(object);
}

Json get JsonArray from JsonArray

I have the following Json. link
I would like to get image_hall_list and image_place_list all url value.
I tried with the following code but no any result.
JSONObject JO = new JSONObject(result);
JSONArray ja = JO.getJSONArray("image_place_list"); //get the array
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = null;
try {
jo = ja.getJSONObject(i);
jsonurl.add(jo.getString("url"));
} catch (JSONException e1) {
e1.printStackTrace();
}
}
Try this:
JSONObject JO = new JSONObject(result);
JSONArray ja = JO.getJSONArray("place_list"); //get the array
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = null;
try {
jo = ja.getJSONObject(i);
JSONArray imageHallList = jo.getJSONArray("image_hall_list");
for (int j = 0; j < imageHallList.length(); j++) {
JSONObject oneImageHallList = imageHallList.getJSONObject(j);
jsonurl.add(oneImageHallList.getString("url"));
}
JSONArray imagePlaceList = jo.getJSONArray("image_place_list");
for (int j = 0; j < imagePlaceList.length(); j++) {
JSONObject oneImagePlaceList = imagePlaceList.getJSONObject(j);
jsonurl.add(oneImagePlaceList.getString("url"));
}
} catch (JSONException e1) {
e1.printStackTrace();
}
}
I would recommend some methods.
One to extract all the URLs for a given object.
public ArrayList<String> getURLs(JSONObject jo, String key) throws JSONException {
List<String> urls = new ArrayList<String>();
JSONArray arr = jo.getJSONArray(key);
for (int j = 0; j < arr.length(); j++) {
JSONObject innerObj = arr.getJSONObject(j);
urls.add(innerObj.getString("url"));
}
return urls;
}
Then, you can use that twice for the respective keys. You also need to first get "place_list" based if your result variable is directly from that link.
try {
JSONObject jsonResponse = new JSONObject(result);
JSONArray ja = jsonResponse.getJSONArray("place_list"); //get the array
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = ja.getJSONObject(i);
jsonurl.addAll(getURLs(jo, "image_hall_list"));
jsonurl.addAll(getURLs(jo, "image_place_list"));
}
} catch (JSONException e1) {
e1.printStackTrace();
}
Use a nested for-loop. First grab the items before the image_hall_list and image_place_list, and then once you have the values stored, loop through image_hall_list, and image_place_list by getting that JSON object and loop through the elements in the JSON objects.

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

Putting data from server into a string array

I am getting data from an array from server . Now I want to put the data received (name of the users ) into an string array . I do not know to put data into a string array.
code:-
JSONObject jo = new JSONObject(data);
Log.d("response", jo.toString(4));
JSONArray jArray = jo.getJSONArray("driver_details");
for (int i=0; i < jArray.length(); i++)
{
System.out.println("please print my name...for....");
try {
JSONObject jsob = jArray.getJSONObject(i);
// Pulling items from the array
mfname = jsob.getString("first_name");
}
}
You have several choices. One being using String[] or ArrayList();
JSONObject jo = new JSONObject(data);
Log.d("response", jo.toString(4));
JSONArray jArray = jo.getJSONArray("driver_details");
String[] names = new String[jArray.length()];
for (int i=0; i < jArray.length(); i++)
{
System.out.println("please print my name...for....");
try {
JSONObject jsob = jArray.getJSONObject(i);
// Pulling items from the array
mfname = jsob.getString("first_name");
names[i] = mfname;
}
}
You can use ArrayList() too:
List<String> names= new ArrayList<String>();
....
names.add(mfname);
Try this..
For String array
String[] array_items = new String[jArray.length()];
for (int i=0; i < jArray.length(); i++)
{
try{
JSONObject jsob = jArray.getJSONObject(i);
array_items[i] = jsob.getString("first_name");
} catch (JSONException e) {
e.printStackTrace();
}
}
For String ArrayList
ArrayList<String> arraylist_items = new ArrayList<String>();
for (int i=0; i < jArray.length(); i++)
{
try{
JSONObject jsob = jArray.getJSONObject(i);
arraylist_items.add(jsob.getString("first_name"));
}catch (JSONException e) {
e.printStackTrace();
}
}
Try something like below code:
For Arraylist:
JSONObject jo = new JSONObject(data);
JSONArray jArray = jo.getJSONArray("driver_details");
ArrayList<String> users = new ArrayList<String>();
for (int i=0; i < jArray.length(); i++)
{
try {
JSONObject jsob = jArray.getJSONObject(i);
String mfname = jsob.getString("first_name");
users.add(mfname);
}
catch (JSONException e) {
e.printStackTrace();
}
}
For String array:
JSONObject jo = new JSONObject(data);
JSONArray jArray = jo.getJSONArray("driver_details");
String[] users = new String[jArray.length()];
for (int i=0; i < jArray.length(); i++)
{
try {
JSONObject jsob = jArray.getJSONObject(i);
String mfname = jsob.getString("first_name");
users[i] = mfname;
}
catch (JSONException e) {
e.printStackTrace();
}
}

Categories

Resources