【问题标题】:how to send json array within json object type of data in post request to server in android如何在post请求中将json对象类型的数据中的json数组发送到android中的服务器
【发布时间】:2017-10-09 06:13:51
【问题描述】:

如何将 json 对象类型的数据在 post 请求中发送到 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"
              }
            ]

}

【问题讨论】:

标签: java android json


【解决方案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;
}

}

使用这两个 Class.Load 数据到这个类。并使用 GSON 库。

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

【讨论】:

    【解决方案2】:
        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 回答

    【讨论】:

      【解决方案3】:
          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 发送到服务器。

      【讨论】:

        猜你喜欢
        • 2017-09-18
        • 2016-12-15
        • 1970-01-01
        • 2015-06-23
        • 2021-10-07
        • 2018-12-08
        • 1970-01-01
        • 2018-09-24
        • 2016-09-29
        相关资源
        最近更新 更多