我想递归地解析包含许多复杂元素集的示例Json文件。 而我想要的代码是这样的:使用Jackson Json解析器递归解析json文件
public class Jsonex {
public static void main(String argv[]) {
try {
Jsonex jsonExample = new Jsonex();
jsonExample.testJackson();
} catch (Exception e){
System.out.println("Exception " + e);
}
}
public static void testJackson() throws IOException {
JsonFactory factory = new JsonFactory();
// System.out.println("hello");
ObjectMapper mapper = new ObjectMapper(factory);
File from = new File("D://albumList.txt");
TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>() {};
HashMap<String,Object> o= mapper.readValue(from, typeRef);
// System.out.println("" + o);
Iterator it = o.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
HashMap<String,Object> o1=mapper.readValue(pairs.getValue().toString(),typeRef);
System.out.println("hey"+o1);
Iterator it1 = o1.entrySet().iterator();
while (it1.hasNext()) {
Map.Entry pairs1 = (Map.Entry)it.next();
System.out.println(pairs1.getKey() + " = " + pairs1.getValue());
it1.remove(); // avoids a ConcurrentModificat
}
}
}}
和我得到这个异常:
异常org.codehaus.jackson.JsonParseException:意外的字符( 'I'(代码105)):期待双引号开始字段名称 at [Source:[email protected];行:1,列:3]
其实我试图做的是,解析文件并获取名称对象对的列表,并采取inturn具有名称 - 对象对的对象。 - 但问题是解析器在字符串之前期待“”!
你能展示一些JSON吗?为什么它是无效的?至少对于字段名称,您可以配置[JsonParser.Feature ALLOW_UNQUOTED_FIELD_NAMES](http://fasterxml.github.com/jackson-core/javadoc/2.1.0/com/fasterxml/jackson/core/JsonParser.Feature.html#ALLOW_UNQUOTED_FIELD_NAMES )在ObjectMapper上。 – nutlike 2013-03-18 15:25:58
{ “菜单”:{ “ID”: “文件”, “值”: “文件”, “弹出”:{ “菜单项”: { “值”: “新”, “点击” :“CreateNewDoc()”} {“value”:“Open”,“onclick”:“OpenDoc()”}, {“value”:“Close”,“onclick”:“CloseDoc()”} ] }} }} 感谢这么多,但工作,但有一个新的异常。它说 异常org.codehaus.jackson.JsonParseException:意外的字符('='(代码61)):期待一个冒号单独的字段名称和值 at [Source:[email protected];行:1,列:5] 现在。请帮助我@nutlike – sreeraag 2013-03-18 16:57:26
当我要求JSON时,我的意思是**您的** JSON而不是http://json.org/example.html ...请相应地更新您的问题。除此之外(再次):为什么你的JSON无效?你从哪里得到它? – nutlike 2013-03-18 17:20:58