jsonObject和jsonArrayList定义一个json串

        ArrayList<ShenPin> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new ShenPin("商品" + i, 20 + i, R.mipmap.ccc));
        }
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (ShenPin sp : list) {
            JSONObject jsonObject1 = new JSONObject();
            try {
                jsonObject1.put("name", sp.getName());
                jsonObject1.put("jiage", sp.getAge());
                jsonObject1.put("img", sp.getImg());

            } catch (JSONException e) {
                e.printStackTrace();
            }
            jsonArray.put(jsonObject1);

        }
        try {
            jsonObject.put("shengpin", jsonArray);
        } catch (JSONException e) {
            e.printStackTrace();
        }

猜你喜欢

转载自blog.csdn.net/weixin_42376563/article/details/81227198