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

时间:2017-10-09 06:13:52

标签: java android json

如何在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"
              }
            ]

}

3 个答案:

答案 0 :(得分: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();

将字符串发送到服务器

了解POST的REST API Sending HTTP POST Request In Java参考文章:Ferrybig回答

答案 1 :(得分: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())

将obj发送到服务器。

答案 2 :(得分:0)

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

}

将此两个Class.Load数据用于此类。并使用GSON Library。

Gson gson= new GSON();
String requiredJson= gson.toJson(testClassObject);