【问题标题】:How to convert List View items to Json Format [duplicate]如何将列表视图项目转换为 Json 格式 [重复]
【发布时间】:2018-06-24 20:45:09
【问题描述】:

我正在开发一个食物预订应用程序。该应用程序有一个购物车,它在列表视图中列出了购物车。如何从购物车列表视图中获取所有值 JSON格式。下面是一个示例格式,我的数据应该是这样的。

{
    "customer_id":"12",
    "product_details": [
                       {"proid":"1", "proname":"abc"},
                       {"proid":"1", "proname":"abc"},
                       {"proid":"3", "proname":"hh"},
                       {"proid":"4", "proname":"gg"}
                   ]
}

【问题讨论】:

  • 参见JSONObject(或JsonWriter用于流式传输)类文档

标签: android json


【解决方案1】:

像 Gson 这样的 JSON 序列化库非常适合。只需注释您的模型类:

@SerializedName("customer_id") private int customerId;
@SerializedName("product_details") private List<ProductDetails> productDetails;

// etc

【讨论】:

    【解决方案2】:

    使用GSON阅读文档。

    【讨论】:

      【解决方案3】:

      添加以下依赖

      compile 'com.google.code.gson:gson:2.8.1'
      

      然后使用

      Gson gson = new Gson(); 
      gson.toJson(data)
      

      【讨论】:

        【解决方案4】:

        // 试试这段代码

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

        【讨论】:

        • 如何发送到服务器
        • @niyasnazar 编辑了我的答案,请立即查看
        • 注意:我已经使用了 volley 库向服务器发送请求。
        • 什么是“alSets”?
        • 感谢您的更新
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多