如何将jsonarray反序列化为List< Map>在java中使用flexjson.deserializer? [英] How to deserialize a jsonarray into a List<Map> in java using flexjson.deserializer?
问题描述
在客户端,我构建了一个像这样的JSOnARRAY:
In the client side, I have constructed a JSOnARRAY like this:
{"filterEntries":[{"dataName":"mainContact","filterValue":"BILLGATES"}]}.
在服务器端(java),我可以使用以下方法退出值:
On the server side (java), I can retireve the values using :
jfilter = JSONValue.parse(jsonFilterStr); //jsonFilterStr={"filterEntries":[{"dataName":"mainContact","filterValue":"BILLGATES"}]}.
JSONArray jFilterEntries = (JSONArray) jfilter.get("filterEntries");
for (int i=0;i<jFilterEntries.size();i++){
JSONObject jFilterEntry = (JSONObject) jFilterEntries.get(i);
String dataName = (String) jFilterEntry.get("dataName");
String filterValue = (String) jFilterEntry.get("filterValue");
}
但是现有应用程序正在使用flex.json.deserializer而我无法使用flex.json.deserializer实现相同的功能。我该怎么办?
我希望这样做:
But the existing app is using flex.json.deserializer and I am unable to achieve the same using flex.json.deserializer. How should I proceed? I wish to do something like this:
JSONDeserializer jsonDeserializer = new JSONDeserializer();
jsonDeserializer.use(null, List.class);
List<Map<String,String>> lMap= (List<Map<String,String>>)jsonDeserializer.deserialize(params);
推荐答案
记住包装数组的顶级对象。你也必须处理它。你必须告诉它期望列表中的Map。为此,您必须使用路径表达式values指定列表中包含的类型。
Remember the top object that wraps the array. You have to handle that as well. You have to tell it to expect a Map inside the List. To do that you have to specify the type contained in the list by using the path expression "values".
Map<String,List<Map<String,String>>> result = new JSONDeserializer<Map<String,List<Map<String,String>>>>()
.use("values",List.class)
.use("values.values", Map.class)
.deserialize( json);
List<Map<String,String>> filterEntries = result.get("filterEntries");
更新:添加新关键字,并在右边匹配左边。
Updated: Add the new keyword, and made the generic types on the right match the left.
这篇关于如何将jsonarray反序列化为List< Map>在java中使用flexjson.deserializer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!