如何使Jackson解析以下JavaScript对象?
{
// this is a comment
x: '1',
y: {
z: '2'
}
}
请注意,上面的示例对象不是实际的JSON - 它是一个JavaScript对象,(1)没有引用属性名称,(2)使用单引号,而不是双引号,并且(3) )包含评论。
用例:我需要解析嵌入在HTML中的JavaScript对象。我可以自己获取JavaScript对象,但我现在需要解析它。
答案 0 :(得分:4)
您可以尝试使用某些parser options:
private static final ObjectMapper om = new ObjectMapper();
om.configure(JsonGenerator.Feature.ALLOW_SINGLE_QUOTES, true);
om.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
om.configure(JsonParser.Feature.ALLOW_COMMENTS, true);