当用执行下面的代码的时候
String json = "{name:\"chenhailong\"}";
Map map = new HashMap();
ObjectMapper mapper = new ObjectMapper();
map = mapper.readValue(json, new TypeReference>(){});
就会出现下面的错误
org.codehaus.jackson.JsonParseException:
Unexpected character ('n' (code 110)): was expecting double-quote to start field name
at [Source: java.io.StringReader@7c87c24a; line: 1, column: 3]
下面提出解决方案
package com.mkyong.jsonexample;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
public class JsonMapExample {
public static void main(String[] args) {
String json = "{name:\"mkyong\"}";
Map map = new HashMap();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
try {
map = mapper.readValue(json,
new TypeReference>(){});
System.out.println(map);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}