请帮我。我试图将值从arraylist的值放到json数组中,但是不幸的是,我不知道为什么,我的json数组仅获取arraylist的最后一个值。我期望输出
[{"Id":"1"},{"Id":"2"},{"Id":"3"}]
但这给了我
[{"Id":"3"},{"Id":"3"},{"Id":"3"}]
你能帮我摆脱这个问题吗?在此先感谢,这是我的代码:
try
{
JSONObject jObject = new JSONObject();
JSONObject brand = new JSONObject();
JSONObject consumerSegments = new JSONObject();
JSONArray jArrayReportUpload = new JSONArray();
try {
JSONArray jArraySubrands = new JSONArray();
JSONArray jArrayConsumerSegments = new JSONArray();
for (int i = 0; i < arraylist_SUBBRANDS.size(); i++)
{
brand.put("Id", _brands.get(i));
Log.d("Brand: " + i, _brands.get(i));
jArraySubrands.put(brand);
}
Log.d("JSONbrand", jArraySubrands.toString());
for (int j = 0; j < arraylist_SEGMENTS.size(); j++) {
consumerSegments.put("Id",_segments.get(j));
Log.d("Segment: " + j, _segments.get(j));
jArrayConsumerSegments.put(consumerSegments);
}
Log.d("JSONsegment", jArrayConsumerSegments.toString());
放 JSONObject consumerSegments = new JSONObject();
作为里面的第一条陈述
for (int j = 0; j < arraylist_SEGMENTS.size(); j++) {
而不是当前位置,您只是用相同的值覆盖内存块,而不是每次都创建一个新实例来保存新值
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句