任何人都可以帮助我解决这个问题,
如何使用JSON
哈希集从JAVA
中删除重复的对象?在这里,我想从JSON
{
"others":[
{
"id":"1",
"caption":"test"
},
{
"id":"2",
"caption":"self"
},
{
"id":"2",
"caption":"Self"
},
{
"id":"1",
"caption":"test"
}
],
"quantity":[
{
"id":"1",
"caption":"self1"
},
{
"id":"1",
"caption":"self1"
}
]}
答案 0 :(得分:1)
下面的代码解析你的json并将它放在HashMap中以获取唯一元素
array_push($household_affected, $row[0],$row[1]);
获得如下输出
HashMap<String,String> hashOut = new HashMap<String,String>();
String input = "{\"others\":[ { \"id\":\"1\", \"caption\":\"test\" }, { \"id\":\"2\", \"caption\":\"self\" }, { \"id\":\"2\", \"caption\":\"self\" }, { \"id\":\"1\", \"caption\":\"test\" }],\"quantity\":[ { \"id\":\"1\", \"caption\":\"self1\" }, { \"id\":\"1\", \"caption\":\"self1\" }]}";
JSONObject jsonObj = new JSONObject(input);
Iterator it = jsonObj.keys();
while (it.hasNext()) {
hashOut.clear();
JSONArray jsInner = (JSONArray) jsonObj.getJSONArray((String) it.next());
for (int i=0;i<jsInner.length();i++) {
JSONObject jo = new JSONObject(jsInner.get(i).toString());
hashOut.put((String)jo.get("id"), (String)jo.get("caption"));
}
System.out.println(hashOut);
}
答案 1 :(得分:0)
通过迭代hashset对象来映射JSON值