<pre name="code" class="html"></**把一个list转换称json格式**/>
JSONArray array = new JSONArray();
JSONObject object;
for (int i = 0; i < list.size(); i++) {
object = new JSONObject();
try {
object.put("goodsId", infos.get(i).getGoodsId());
object.put("cartId", infos.get(i).getCartId());
object.put("num", "0");
array.put(object);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String json=array.toString();
另一种简便方法:
添加Gson依赖:
compile 'com.google.code.gson:gson:2.6.2'
String json = new Gson().toJson(list);