简体   繁体   English

如何在post请求中将json对象类型数据中的json数组发送到android中的服务器

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

how to send json array within json object type of data in post request to server in android如何在post请求中将json对象类型数据中的json数组发送到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"
              }
            ]

}
    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学习 REST API for POST 在 Java 中发送 HTTP POST 请求参考帖子:Ferrybig answer

-----------------------------------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.使用这两个 Class.Load 数据到这个类。 And use GSON Library.并使用 GSON 库。

Gson gson= new GSON();
String requiredJson= gson.toJson(testClassObject);
    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.将 obj 发送到服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM