-3

how to send json array within json object type of data in post request to server in android

{
   "user_id":"1",
   "username":"shubham",
   "order":  [
               {"product_id":"2",
                "qty":"5",
                "price":"100",
                "total":"500",
                "product_name":"choclate"
               },
              {"product_id":"1",
               "qty":"2",
               "price":"50",
               "total":"500",
               "product_name":"choclate"
              }
            ]

}
Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
Saloni Palod
  • 31
  • 1
  • 3

3 Answers3

1
    ArrayList<JSONObject> arrayList = new ArrayList<JSONObject>();
    JSONObject postedJSON = new JSONObject();

    try {
        objJSON.put("key", "value");
      // add key value for json formati
    } catch (JSONException e) {
        e.printStackTrace();
    }
    arrayList.add(postedJSON);
    String variable = arrayList.toString();

Send the string to the server

Learn REST API for POST Sending HTTP POST Request In Java Reference post: Ferrybig answer

Priya Rajan
  • 687
  • 8
  • 21
1
-----------------------------------test.Order.java-----------------------------------

package test;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Order {

@SerializedName("product_id")
@Expose
private String productId;
@SerializedName("qty")
@Expose
private String qty;
@SerializedName("price")
@Expose
private String price;
@SerializedName("total")
@Expose
private String total;
@SerializedName("product_name")
@Expose
private String productName;

public String getProductId() {
return productId;
}

public void setProductId(String productId) {
this.productId = productId;
}

public String getQty() {
return qty;
}

public void setQty(String qty) {
this.qty = qty;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}

public String getTotal() {
return total;
}

public void setTotal(String total) {
this.total = total;
}

public String getProductName() {
return productName;
}

public void setProductName(String productName) {
this.productName = productName;
}

}
-----------------------------------test.TestClass.java-----------------------------------

package test;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class TestClass {

@SerializedName("user_id")
@Expose
private String userId;
@SerializedName("username")
@Expose
private String username;
@SerializedName("order")
@Expose
private List<Order> order = null;

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public List<Order> getOrder() {
return order;
}

public void setOrder(List<Order> order) {
this.order = order;
}

}

Use this two Class.Load data to this class. And use GSON Library.

Gson gson= new GSON();
String requiredJson= gson.toJson(testClassObject);
taman neupane
  • 938
  • 9
  • 18
0
    JSONObject obj=new JSONObject();
    obj.put("user_id",1);
    obj.put("username","shubham");

    JSONArray array=new JSONArray();

    JSONObject objp=new JSONObject();
    // use for loop in case of multiple prodcuts
    objp.put("product_id","2");
    objp.put("qty","5");
    objp.put("product_id","2");
    objp.put("price","100");
    objp.put("total","500");
    objp.put("product_name","choclate");

    array.put(objp.toString());
    obj.put("order",array.toString())

send obj to server.

D.J
  • 1,439
  • 1
  • 12
  • 23