java-将JSONObject转换为HashMap<;字符串,字符串>;
我有一个java-将JSONObject转换为HashMap<;字符串,字符串>;,java,android,json,hashmap,Java,Android,Json,Hashmap,我有一个JSONObject结构如下: "data": { "title": "Pool Party", "date": { "start": "2018-08-14T15:44:44.625Z", "end": "2018-08-14T18:44:44.625Z" } Type type = new TypeToken<Map<String, String>>(){}.getType(); HashMap<String,
JSONObject
结构如下:
"data": {
"title": "Pool Party",
"date": {
"start": "2018-08-14T15:44:44.625Z",
"end": "2018-08-14T18:44:44.625Z"
}
Type type = new TypeToken<Map<String, String>>(){}.getType();
HashMap<String, String> params = Gson().fromJson(jsonString, type);
public void testJackson() throws IOException {
ObjectMapper mapper = new ObjectMapper();
String json = "{\"data\": {\"title\": \"Pool Party\",\"date\": {\"start\":\"2018-08-14T15:44:44.625Z\", \"end\":\"2018-08-14T18:44:44.625Z\"}}}";
TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
};
HashMap<String, Object> o = mapper.readValue(from, typeRef);
System.out.println("Got " + o);
}
我想把它转换成
HashMap<String, String>
但我有一个错误:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT
可能是因为我的JSON
字符串的结构
有没有办法得到这样的东西?
谢谢您的帮助。您可以使用ObjectMapper对其进行转换
public void testJackson() throws IOException {
ObjectMapper mapper = new ObjectMapper();
String json = "{"data": {"title": "Pool Party","date": {"start": "2018-08-14T15:44:44.625Z", "end": "2018-08-14T18:44:44.625Z"}}}";
TypeReference<HashMap<String,Object>> typeRef
= new TypeReference<HashMap<String,Object>>() {};
HashMap<String,Object> o = mapper.readValue(from, typeRef);
System.out.println("Got " + o);
}
public void testJackson()引发IOException{
ObjectMapper mapper=新的ObjectMapper();
字符串json=“{”data:{”title:“Pool Party”,“date:{”start:“2018-08-14T15:44:44.625Z”,“end:“2018-08-14T18:44:44.625Z”}};
类型引用类型引用
=新类型引用(){};
HashMap o=mapper.readValue(from,typeRef);
System.out.println(“Got”+o);
}
尝试以下方法:
"data": {
"title": "Pool Party",
"date": {
"start": "2018-08-14T15:44:44.625Z",
"end": "2018-08-14T18:44:44.625Z"
}
Type type = new TypeToken<Map<String, String>>(){}.getType();
HashMap<String, String> params = Gson().fromJson(jsonString, type);
public void testJackson() throws IOException {
ObjectMapper mapper = new ObjectMapper();
String json = "{\"data\": {\"title\": \"Pool Party\",\"date\": {\"start\":\"2018-08-14T15:44:44.625Z\", \"end\":\"2018-08-14T18:44:44.625Z\"}}}";
TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
};
HashMap<String, Object> o = mapper.readValue(from, typeRef);
System.out.println("Got " + o);
}
public void testJackson()引发IOException{
ObjectMapper mapper=新的ObjectMapper();
字符串json=“{\'data\':{\'title\':\'Pool Party\',\'date\':{\'start\':\'2018-08-14T15:44:44.625Z\',\'end\':\'2018-08-14T18:44:44.625Z\'}}”;
TypeReference typeRef=新的TypeReference(){
};
HashMap o=mapper.readValue(from,typeRef);
System.out.println(“Got”+o);
}
这里的重复问题已经给出了一个解决方案,这不完全是我想要的@himly,但我需要将其转换为HashMap而不是HashMap