Android 如何根据对象的另一个值获取特定的JSON对象?
我使用的json数组如下所示:Android 如何根据对象的另一个值获取特定的JSON对象?,android,json,object,Android,Json,Object,我使用的json数组如下所示: {"GetReportResult": [ {"bulan":"4","total":"2448","type":"CHEESE1K","uang":"8847823"}, {"bulan":"4","total":"572476","type":"ESL","uang":"5863408410"}, {"bulan":"4","total":"46008","type":"ESL500ML","uang":"234498301"},
{"GetReportResult":
[
{"bulan":"4","total":"2448","type":"CHEESE1K","uang":"8847823"},
{"bulan":"4","total":"572476","type":"ESL","uang":"5863408410"},
{"bulan":"4","total":"46008","type":"ESL500ML","uang":"234498301"},
{"bulan":"5","total":"5703","type":"CHEESE1K","uang":"134929306"},
{"bulan":"5","total":"648663","type":"ESL","uang":"6645764498"},
{"bulan":"5","total":"51958","type":"WHP","uang":"631994613"},
{"bulan":"6","total":"6190","type":"CHEESE1K","uang":"104527773"},
{"bulan":"6","total":"443335","type":"ESL","uang":"4540123082"},
{"bulan":"6","total":"30550","type":"ESL500ML","uang":"148302378"},
]
}
total1 | total2 | total3
----------------------------------
2448 | 2448 | 2448
572476 | 572476 | 572476
46008 | 46008 | 46008
total1 | total2 | total3
----------------------------------
6190 | 5703 | 2448
443335 | 648663 | 572476
30550 | 51958 | 46008
ArrayList < String > listBulan4 = new ArrayList < String > ();
listBulan4.add(object.getString("total"));
这是我想要得到的特定对象的代码:
JSONArray weeklyflash = json.getJSONArray("GetReportResult");
JSONObject e = weeklyflash.getJSONObject(i);
String bulan = e.getString("bulan");
if (bulan == 6){
map.put("total1", e.getString("total")); // I want the "total" values are from specified bulan (e.g. bulan == 6)
}
else if (bulan == 5){
map.put("total2", e.getString("total")); // I want the "total" values are from specified bulan (e.g. bulan == 5)
}
else if (bulan == 4){
map.put("total3", e.getString("total")); // I want the "total" values are from specified bulan (e.g. bulan == 4)
}
我得到的是这样的:
{"GetReportResult":
[
{"bulan":"4","total":"2448","type":"CHEESE1K","uang":"8847823"},
{"bulan":"4","total":"572476","type":"ESL","uang":"5863408410"},
{"bulan":"4","total":"46008","type":"ESL500ML","uang":"234498301"},
{"bulan":"5","total":"5703","type":"CHEESE1K","uang":"134929306"},
{"bulan":"5","total":"648663","type":"ESL","uang":"6645764498"},
{"bulan":"5","total":"51958","type":"WHP","uang":"631994613"},
{"bulan":"6","total":"6190","type":"CHEESE1K","uang":"104527773"},
{"bulan":"6","total":"443335","type":"ESL","uang":"4540123082"},
{"bulan":"6","total":"30550","type":"ESL500ML","uang":"148302378"},
]
}
total1 | total2 | total3
----------------------------------
2448 | 2448 | 2448
572476 | 572476 | 572476
46008 | 46008 | 46008
total1 | total2 | total3
----------------------------------
6190 | 5703 | 2448
443335 | 648663 | 572476
30550 | 51958 | 46008
ArrayList < String > listBulan4 = new ArrayList < String > ();
listBulan4.add(object.getString("total"));
我想从指定的对象值中获取值(例如,我想从“total”
中获取所有值,其中“bulan”
=5)。怎么做?
我想要的是这样的:
{"GetReportResult":
[
{"bulan":"4","total":"2448","type":"CHEESE1K","uang":"8847823"},
{"bulan":"4","total":"572476","type":"ESL","uang":"5863408410"},
{"bulan":"4","total":"46008","type":"ESL500ML","uang":"234498301"},
{"bulan":"5","total":"5703","type":"CHEESE1K","uang":"134929306"},
{"bulan":"5","total":"648663","type":"ESL","uang":"6645764498"},
{"bulan":"5","total":"51958","type":"WHP","uang":"631994613"},
{"bulan":"6","total":"6190","type":"CHEESE1K","uang":"104527773"},
{"bulan":"6","total":"443335","type":"ESL","uang":"4540123082"},
{"bulan":"6","total":"30550","type":"ESL500ML","uang":"148302378"},
]
}
total1 | total2 | total3
----------------------------------
2448 | 2448 | 2448
572476 | 572476 | 572476
46008 | 46008 | 46008
total1 | total2 | total3
----------------------------------
6190 | 5703 | 2448
443335 | 648663 | 572476
30550 | 51958 | 46008
ArrayList < String > listBulan4 = new ArrayList < String > ();
listBulan4.add(object.getString("total"));
使用地图
地图中的关键点是您希望总计的bulan值
迭代数组并更新相关bulan值的和。我认为这一行会产生问题 布朗=6 这样试试 布朗等于(“6”) 我想这对你有帮助
谢谢我觉得你的代码很好。 试试看
if (bulan.equals("6")){
map.put("total1", e.getString("total")); // I want the "total" values are from specified bulan (e.g. bulan == 6)
}
else if (bulan.equals("5")){
map.put("total2", e.getString("total")); // I want the "total" values are from specified bulan (e.g. bulan == 5)
}
else if (bulan.equals("4")){
map.put("total3", e.getString("total")); // I want the "total" values are from specified bulan (e.g. bulan == 4)
}
给你
try {
ArrayList < String > listBulan4 = new ArrayList < String > ();
ArrayList < String > listBulan5 = new ArrayList < String > ();
ArrayList < String > listBulan6 = new ArrayList < String > ();
JSONObject jsonObject = new JSONObject("jsonString");
JSONArray array = jsonObject.getJSONArray("GetReportResult");
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
int bulanValue = Integer.parseInt(object.getString("bulan"));
switch (bulanValue) {
case 4:
listBulan4.add(object.getString("total"));
break;
case 5:
listBulan5.add(object.getString("total"));
break;
case 6:
listBulan6.add(object.getString("total"));
break;
}
}
Map < String, ArrayList < String >> map = new HashMap < String, ArrayList < String >> ();
map.put("total1", listBulan6);
map.put("total2", listBulan5);
map.put("total3", listBulan4);
} catch (Exception e) {}
试试看{
ArrayListlistBulan4=新的ArrayList();
ArrayListlistBulan5=新的ArrayList();
ArrayListlistBulan6=新的ArrayList();
JSONObject JSONObject=新的JSONObject(“jsonString”);
JSONArray数组=jsonObject.getJSONArray(“GetReportResult”);
对于(int i=0;i>Map=newhashmap>();
map.put(“总计1”,列表6);
map.put(“总计2”,列表5);
map.put(“总计3”,列表4);
}捕获(例外e){}
下面是代码
package org.sample;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class SOActivity extends Activity {
String string = "{\"GetReportResult\":[{\"bulan\":\"4\",\"total\":\"2448\",\"type\":\"CHEESE1K\",\"uang\":\"8847823\"},{\"bulan\":\"4\",\"total\":\"572476\",\"type\":\"ESL\",\"uang\":\"5863408410\"},{\"bulan\":\"4\",\"total\":\"46008\",\"type\":\"ESL500ML\",\"uang\":\"234498301\"},{\"bulan\":\"5\",\"total\":\"5703\",\"type\":\"CHEESE1K\",\"uang\":\"134929306\"},{\"bulan\":\"5\",\"total\":\"648663\",\"type\":\"ESL\",\"uang\":\"6645764498\"},{\"bulan\":\"5\",\"total\":\"51958\",\"type\":\"WHP\",\"uang\":\"631994613\"},{\"bulan\":\"6\",\"total\":\"6190\",\"type\":\"CHEESE1K\",\"uang\":\"104527773\"},{\"bulan\":\"6\",\"total\":\"443335\",\"type\":\"ESL\",\"uang\":\"4540123082\"},{\"bulan\":\"6\",\"total\":\"30550\",\"type\":\"ESL500ML\",\"uang\":\"148302378\"}]}";
Map < String, ArrayList < String >> map;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
map = new HashMap < String, ArrayList < String >> ();
try {
ArrayList < String > listBulan4 = new ArrayList < String > ();
ArrayList < String > listBulan5 = new ArrayList < String > ();
ArrayList < String > listBulan6 = new ArrayList < String > ();
JSONObject jsonObject = new JSONObject(string);
JSONArray array = jsonObject.getJSONArray("GetReportResult");
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
int bulanValue = Integer.parseInt(object.getString("bulan"));
switch (bulanValue) {
case 4:
listBulan4.add(object.getString("total"));
break;
case 5:
listBulan5.add(object.getString("total"));
break;
case 6:
listBulan6.add(object.getString("total"));
break;
}
}
map.put("total1", listBulan6);
map.put("total2", listBulan5);
map.put("total3", listBulan4);
} catch (Exception e) {
e.printStackTrace();
}
Log.i("test", map.toString());
}
}
package org.sample;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Map;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.Bundle;
导入android.util.Log;
公共类活动扩展了活动{
“总人数”是指“2448\”,“类型”是指,“类型”是指“CheSE11k,”,”,”,”,”,”,”,”类型类型是指“类型”是指“中国的一个1k\”,“黄”是指“884747823\”,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,布兰:“:“5\”,“总计:”“5703\”,“类型:”“奶酪1K\”,“黄”\“134929306\”,,,,,,“总人数”是指“648663\”,“类型”是指“ESL\”,“黄”是指“ESL\”,,“黄”是指“664592929306\”,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,“总人数“:”6“,”总计“:”443335“,”类型“:”ESL“,”黄“:”4540123082“,“{”布朗“:\“6\”、“总计\”:“30550\”、“类型\”:“ESL500ML\”、“黄\”:“148302378\”});
Map>Map;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
map=newhashmap>();
试一试{
ArrayListlistBulan4=新的ArrayList();
ArrayListlistBulan5=新的ArrayList();
ArrayListlistBulan6=新的ArrayList();
JSONObject JSONObject=新的JSONObject(字符串);
JSONArray数组=jsonObject.getJSONArray(“GetReportResult”);
对于(int i=0;i
您的代码似乎正确
哈希映射不允许重复密钥。您正在尝试放置重复密钥
因此,请尝试使用ArrayList
诸如此类的事情:
{"GetReportResult":
[
{"bulan":"4","total":"2448","type":"CHEESE1K","uang":"8847823"},
{"bulan":"4","total":"572476","type":"ESL","uang":"5863408410"},
{"bulan":"4","total":"46008","type":"ESL500ML","uang":"234498301"},
{"bulan":"5","total":"5703","type":"CHEESE1K","uang":"134929306"},
{"bulan":"5","total":"648663","type":"ESL","uang":"6645764498"},
{"bulan":"5","total":"51958","type":"WHP","uang":"631994613"},
{"bulan":"6","total":"6190","type":"CHEESE1K","uang":"104527773"},
{"bulan":"6","total":"443335","type":"ESL","uang":"4540123082"},
{"bulan":"6","total":"30550","type":"ESL500ML","uang":"148302378"},
]
}
total1 | total2 | total3
----------------------------------
2448 | 2448 | 2448
572476 | 572476 | 572476
46008 | 46008 | 46008
total1 | total2 | total3
----------------------------------
6190 | 5703 | 2448
443335 | 648663 | 572476
30550 | 51958 | 46008
ArrayList < String > listBulan4 = new ArrayList < String > ();
listBulan4.add(object.getString("total"));
ArrayListlistBulan4=newarraylist();
添加(object.getString(“total”);
好的,我已经更改了它,但是“total”
值不是来自“bulan”
。等于(“6”),它来自所有“bulan”
:(我没有得到它:(你能给我一个更具体的例子吗?你的代码的问题是你正在覆盖映射值。见我的回答值将在循环中被覆盖我得到了这个错误:错误解析数据org.json.JSONException:Value类型为java.lang.String的jsonString不能转换为JSONObject
@blankon91而不是jsonString放入你的jsonString在这里我已经做了,你能检查一下更多细节吗:这是我在我的日志中得到的=06-28 13:50:18.448:I/test(366):{}让我们看看