【发布时间】:2016-01-28 22:55:40
【问题描述】:
我正在尝试解析以下 json。这必须给我一个错误,说不正确的 json 格式。但是解析器只解析 json 直到 "value:15" 并且没有抛出任何异常。我怎样才能做到这一点?
String json = { and : [{key: domain, value: cricket}, {key : STAT_CDE,value : 15}]}, { and : [{key: domain, value: football}, {key : STAT_CDE,value : 10}]}
我正在使用的示例代码:
import org.codehaus.jackson.map.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
mapper.readTree(json); //this line is not throwing me any exception
这里是sn-p的代码:
import org.codehaus.jackson.map.ObjectMapper;
public class JsonTestParse {
public static void main(String[] args) {
String json = "{ \"and\" : [{\"key\": \"domain\", \"op\": \"=\", \"value\": \"cricket\"}, {\"key\" : \"STAT_CDE\",\"op\" : \"=\",\"value\" : \"13\"}]},"+
"{ \"and\" : [{\"key\": \"domain\", \"op\": \"=\", \"value\": \"Football\"}, {\"key\" : \"STAT_CDE\",\"op\" : \"=\",\"value\" : \"10\"}]}";
System.out.println("json: " + json);
ObjectMapper mapper = new ObjectMapper();
try {
mapper.readTree(json);
} catch (Exception e) {
System.out.println("object mapper exp");
}
System.out.println("mapper complete");
}
}
还有输出:
第 1 行:json: { "and" : [{"key": "domain", "op": "=", "value": "cricket"}, {"key" : "STAT_CDE"," op" : "=","value" : "13"}]},{ "and" : [{"key": "domain", "op": "=", "value": "Football"}, {"key" : "STAT_CDE","op" : "=","value" : "10"}]} 第 2 行:映射器完成
【问题讨论】:
-
这不是 JSON,JSON 是一个字符串,你应该双引号属性名称
-
好的。这是我使用的 java json 字符串: { \"and\" : [{\"key\": \"domain\", \"op\": \"=\", \"value\": \ "cricket\"}, {\"key\" : \"STAT_CDE\",\"op\" : \"=\",\"value\" : \"15\"}]},{ \"and \" : [{\"key\": \"domain\", \"op\": \"=\", \"value\": \"football\"}, {\"key\" : \ "STAT_CDE\",\"op\" : \"=\",\"value\" : \"10\"}]}
标签: json parsing objectmapper