我正在尝试递归解析一个包含许多复杂元素集的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
}
}
}}
我遇到了这个异常:
Exception org.codehaus.jackson.JsonParseException: Unexpected character ('i' (code 105)): was expecting double-quote to start field name at [Source: java.io.StringReader@2de7753a; line: 1, column: 3]
实际上我想做的是解析文件并获取名称对象对列表,然后取出其中的对象,该对象又包含名称-对象对。但是问题在于解析器希望在字符串前面加上""!
mapper.readValue(...)
。我添加了一个答案,可能会帮助您重新思考/重新设计您的解决方案。 - nutlike