Android 如何将列表视图项转换为Json格式
我正在开发一个食品预订应用程序。该应用程序有一个购物车,在列表视图中列出点菜项目。如何将购物车列表视图中的所有值获取到 JSON格式。下面是一个示例格式,我的数据应该是这样的Android 如何将列表视图项转换为Json格式,android,json,Android,Json,我正在开发一个食品预订应用程序。该应用程序有一个购物车,在列表视图中列出点菜项目。如何将购物车列表视图中的所有值获取到 JSON格式。下面是一个示例格式,我的数据应该是这样的 { "customer_id":"12", "product_details": [ {"proid":"1", "proname":"abc"}, {"proid":"1", "proname":"abc"},
{
"customer_id":"12",
"product_details": [
{"proid":"1", "proname":"abc"},
{"proid":"1", "proname":"abc"},
{"proid":"3", "proname":"hh"},
{"proid":"4", "proname":"gg"}
]
}
像Gson这样的JSON序列化库很适合这样做。只需对模型类进行注释:
@SerializedName("customer_id") private int customerId;
@SerializedName("product_details") private List<ProductDetails> productDetails;
// etc
@SerializedName(“客户id”)私有int customerId;
@SerializedName(“产品详细信息”)私有列表productDetails;
//等
像Gson这样的JSON序列化库很适合这样做。只需对模型类进行注释:
@SerializedName("customer_id") private int customerId;
@SerializedName("product_details") private List<ProductDetails> productDetails;
// etc
@SerializedName(“客户id”)私有int customerId;
@SerializedName(“产品详细信息”)私有列表productDetails;
//等
用于阅读文档。用于阅读文档。添加以下依赖项
compile 'com.google.code.gson:gson:2.8.1'
然后使用
Gson gson = new Gson();
gson.toJson(data)
添加以下依赖项
compile 'com.google.code.gson:gson:2.8.1'
然后使用
Gson gson = new Gson();
gson.toJson(data)
//试试这个代码
JSONObject input = new JSONObject();
JSONArray array = new JSONArray();
for (int i = 0; i < alSets.size(); i++)
{
JSONObject obj_set1 = new JSONObject();
String setNo= "Set " + String.valueOf(i+1);
obj_set1.put("SetId", i + 1);
array.put(obj_set1);
}
input("Key", input);
JsonObjectRequest request = new JsonObjectRequest(requestURL,input, stringListener, errorListener);
request.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
addToRequestQueue(request);
JSONObject input=new JSONObject();
JSONArray数组=新的JSONArray();
对于(int i=0;i
//试试这段代码
JSONObject input = new JSONObject();
JSONArray array = new JSONArray();
for (int i = 0; i < alSets.size(); i++)
{
JSONObject obj_set1 = new JSONObject();
String setNo= "Set " + String.valueOf(i+1);
obj_set1.put("SetId", i + 1);
array.put(obj_set1);
}
input("Key", input);
JsonObjectRequest request = new JsonObjectRequest(requestURL,input, stringListener, errorListener);
request.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
addToRequestQueue(request);
JSONObject input=new JSONObject();
JSONArray数组=新的JSONArray();
对于(int i=0;i
参见JSONObject
(或JsonWriter
用于流式处理)类文档参见JSONObject
(或JsonWriter
用于流式处理)课堂文档如何发送到server@niyasnazar已编辑我的答案请立即检查注意:我已使用截击库将请求发送到服务器。什么是“alSets”?感谢您的更新如何将其发送到server@niyasnazar我已经编辑了我的答案,请现在检查注意:我已经使用截击库向服务器发送请求。什么是截击库“alSets”?谢谢你的更新