我有一个类似这样的字符串:
[ { "1":33 }, { "2":30 }, { "3":15 }, { "4":23 }, ...{ "9":17 },... { "U":2 }, { "V":22 }, { "W":1 }, { "X":35 }, { "Y":6 }, { "Z":19 } ]
结构为{"key":value}
。我需要将它转换为表/ HashMap,以便我可以根据键获取值。
我尝试使用Gson但失败了。有没有比使用某些序列化更简单的方法?
TIA
答案 0 :(得分:6)
使用我做的这个方法:
public HashMap<String, Integer> convertToHashMap(String jsonString) {
HashMap<String, Integer> myHashMap = new HashMap<String, Integer>();
try {
JSONArray jArray = new JSONArray(jsonString);
JSONObject jObject = null;
String keyString=null;
for (int i = 0; i < jArray.length(); i++) {
jObject = jArray.getJSONObject(i);
// beacuse you have only one key-value pair in each object so I have used index 0
keyString = (String)jObject.names().get(0);
myHashMap.put(keyString, jObject.getInt(keyString));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return myHashMap;
}
使用:
String myString = "[ { \"1\":33 }, { \"2\":30 }, { \"3\":15 }, { \"4\":23 }, { \"9\":17 }, { \"U\":2 }, { \"V\":22 }, { \"W\":1 }, { \"X\":35 }, { \"Y\":6 }, { \"Z\":19 } ]";
HashMap<String, Integer> map = convertToHashMap(myString);
Log.d("test", map.toString());
您还可以使用map.keySet()
答案 1 :(得分:1)
public static void main(String[] args) {
String s = "{ 1:33 }, { 2:30 }, { 3:15 }, { 4:23 }, { 9:17 }, { U:2 }, { V:22 }, { W:1 }, { X:35 }, { Y:6 }, { Z:19 }";
String[] arr = s.split(", ");
Map<String, Integer> map = new HashMap<String, Integer>();
for (String str : arr) {
str = str.replace("{", "").replace("}", "");
String[] splited = str.split(":");
map.put(splited[0], Integer.parseInt(splited[1].trim()));
}
System.out.println(map);
}
输出:
{ 9=17, Z=19, Y=6, X=35, 1=33, 3=15, 2=30, W=1, V=22, 4=23, U=2}
答案 2 :(得分:0)
编写自己的简单解析器。从开始开始阅读每个字符,当你看到“然后开始处理下一个字符开始作为键的开始并且在下一个的出现时”现在将它设为键,然后查找:然后记录值,直到看到空格或者}。再次重复此过程,直到您已读取字符串中的所有字符。这样,如果有n个字符,那么它将在O(n)
中解决答案 3 :(得分:0)
你有一个对象数组,每个对象都不同(它有不同的字段)。在讨论将其反序列化为对象时,JSON解析器会遇到问题。 Gson用户指南specifically mentions this。
你可以使用Gson,但是你将不得不使用他们建议的方法之一。创建返回Map
的自定义反序列化器非常简单。您需要遍历数组,获取每个对象,然后获取字段和值:
// create this class so as not to affect other Map types:
class MyMap extends HashMap<String, Integer> {}
class MyMapDeserializer implements JsonDeserializer<MyMap>
{
public MyMap deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
throws JsonParseException
{
JsonArray ja = je.getAsJsonArray();
MyMap map = new MyMap();
for (int i = 0; i < ja.size(); i++)
{
JsonObject jo = ja.get(i).getAsJsonObject();
Set<Entry<String, JsonElement>> set = jo.entrySet();
for (Entry<String, JsonElement> e : set)
{
map.put(e.getKey(), e.getValue().getAsInt());
}
}
return map;
}
}
您可以通过以下方式使用它:
Gson gson = new GsonBuilder()
.registerTypeAdapter(MyMap.class, new MyMapDeserializer())
.create();
MyMap mm = gson.fromJson(jsonString, MyMap.class);
答案 4 :(得分:0)
您可以使用正则表达式使用此代码:
Map<String, String> map = new HashMap<String, String>();
//!!Your string comes here, I escape double quoutes manually !!
String str = "\"[ { \"1\":33 }, { \"2\":30 }, { \"3\":15 }, { \"4\":23 }, { \"9\":17 }, {\"U\":2 }, { \"V\":22 }, { \"W\":1 }, { \"X\":35 }, { \"Y\":6 }, { \"Z\":19 } ]\"";
String regex = "\\{\\s*\"(\\w+)\":(\\d+)\\s*\\}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
String key = matcher.group(1);
String value = matcher.group(2);
System.out.println(key + " " + value);
map.put(key, value);
}
打印:
1 33
2 30
3 15
4 23
9 17
U 2
V 22
W 1
X 35
Y 6
Z 19
注意:如果您需要Map<String,Integer>
,请更改定义并使用Integer.parseInt()
从字符串中获取整数。