美文网首页Android开发
学习笔记之——Json数据上传

学习笔记之——Json数据上传

作者: 筱宓dawnLing | 来源:发表于2018-01-31 17:07 被阅读19次

(菜鸟级文章,大神绕道通行,勿喷~)移动端开发中难免会上传各种格式的数据给后台,前两天一个刚工作的学妹问我如何上传数组给后台,这么一个简单的问题警醒我应该做个笔记记录一下我上传过的数据格式:

总而言之,不管是什么数据类型,搞不定的就新建个模型类,然后再重写的 toString()方法中写需要上传的数据格式就行了

一.数组格式,内含字典,如下图所示

数组格式,内含字典.png 数组格式,内含字典.png

做法:

首先 新建Bean类SupplyProductsBean .class

public class SupplyProductsBean {

    public String product_id;//产品id
    public String num;//数量

    public SupplyProductsBean(String product_id, String num) {
        this.product_id = product_id;
        this.num = num;
    }

    @Override
    public String toString() {
        return "{" +
                "\"product_id\":\"" + product_id + '\"' +
                ",\"num\":\"" + num + '\"' +
                '}';
    }
}

然后 添加Bean类数据

List<SupplyProductsBean> supplyList = new ArrayList<>();
        for (int i = 0; i < cartList.size(); i++) {
            supplyList.add(new SupplyProductsBean(cartList.get(i).product_id, productlist.get(i).buy_num + ""));
        }

最后通过map上传(用网络框架实现八种上传方式都行)即可

Map<String, Object> map = new HashMap<String, Object>();
        map.put("supply_products", list);

传上去的数据结果:


image.png

二.数组或字符串

数组货字符串.png

做法:

val map = HashMap<String, Any?>()
            map.put("market_id", market_id)
            if (list != null) {
                val ints = IntArray(list.size)
                for (i in list.indices) {
                    ints[i] = list[i].market_product_id
                }
                map.put("market_product_id_data", ints)
            }

相关文章

网友评论

    本文标题:学习笔记之——Json数据上传

    本文链接:https://www.haomeiwen.com/subject/kjttoxtx.html