从地图中重新获取数据时获取双方括号 [英] Getting double square brackets while retriving data from map
本文介绍了从地图中重新获取数据时获取双方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将数据以字符串形式存储在Map中作为键,JSONArray存储为值
但是在检索数据时,我得到值的双方括号
I am storing data inside a Map in form of String as key and JSONArray as Value But while retrieving data i am getting double square brackets for value
这是我的程序
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
public class Test {
public static void main(String args[]) {
Map<String, JsonArray> datafromdb = new LinkedHashMap<String, JsonArray>();
JsonArray jsonarray = new JsonArray();
JsonObject pacs_obj = new JsonObject();
pacs_obj.addProperty("pac_id", "1235");
jsonarray.add(pacs_obj);
String new_catid = "1";
if (!datafromdb.containsKey(new_catid)) {
datafromdb.put(new_catid, new JsonArray());
}
datafromdb.get(new_catid).add(jsonarray);
Iterator it = datafromdb.entrySet().iterator();
JsonArray catarr = new JsonArray();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
JsonArray array = (JsonArray) pair.getValue();
System.out.println(array);
}
}
}
当我运行上述程序时,输出为
when i run the above program the out put is
[[{"pac_id":"1235"}]]
您能否告诉我如何检索此问题
Could you please tell me how to retrieve this issue
推荐答案
你有两个方括号,因为你创建了两个数组:
这里:
you have two square braces, because you've created two arrays: Here:
JsonArray jsonarray = new JsonArray();
JsonObject pacs_obj = new JsonObject();
pacs_obj.addProperty("pac_id", "1235");
jsonarray.add(pacs_obj);
此处:
if (!datafromdb.containsKey(new_catid)) {
datafromdb.put(new_catid, new JsonArray());
}
datafromdb.get(new_catid).add(jsonarray);
只需摆脱一个数组,例如:
Just get rid of one of array, for example:
if (!datafromdb.containsKey(new_catid)) {
datafromdb.put(new_catid, jsonarray);
}
这篇关于从地图中重新获取数据时获取双方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文