我正在尝试递归解析具有许多复杂元素集的示例 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)):期望双引号在 [Source: java.io.StringReader@2de7753a; 开始字段名称;行:1,列:3]
实际上,我想做的是解析文件并获取名称对象对的列表,然后获取具有名称对象对的对象。- 但问题是解析器在字符串之前期待“”!