我写了这个 JSON 代码
public class TestClass {
public static void main(String[] args) {
String inputJson= "{" +"\"contexts\" : { "+ "{ " + "\"times\" : { " + "{ " + "\"end\":17, " + "\"begin\" : 9 " + "}" + "}," + "\"contextname\":\"OFFICE_HOURS\" " + "}," + "{ " + "\"days\": { " + "\"MON\", " + "\"TUE\", " + "\"WED\" , " + "\"THU\", " + "\"FRI\" " + "} , " + "\"contextname\" : \"WORKDAYS\" " + "}";
ObjectMapper mapper= new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
try {
MainParser mp= mapper.readValue(inputJson, MainParser.class);
System.out.println(mp.getContextname());
}
但是我在控制台框中发现了这个错误
org.codehaus.jackson.JsonParseException: Unexpected character ('{' (code 123)): was expecting double-quote to start field name
at [Source: java.io.StringReader@7cf10a6f; line: 1, column: 19]
我需要帮助
最佳答案
格式化后,您的 JSON 如下所示:
{
"contexts": {
{
"times": {
{
"end": 17,
"begin": 9
}
},
"contextname": "OFFICE_HOURS"
},
{
"days": {
"MON",
"TUE",
"WED",
"THU",
"FRI"
},
"contextname": "WORKDAYS"
}
由于多种原因,这是无效的。
您可以通过用数组替换“contexts”的内容并将日期包装在数组中并删除一些额外的大括号来使其有效。
{
"contexts": [
{
"times": {
"end": 17,
"begin": 9
},
"contextname": "OFFICE_HOURS"
},
{
"days": [
"MON",
"TUE",
"WED",
"THU",
"FRI"
],
"contextname": "WORKDAYS"
}
]
}
更好的方法可能是使用“contextname”值作为键并简化整个 JSON。
{
"OFFICE_HOURS": {
"end": 17,
"begin": 9
},
"WORKDAYS": [
"MON",
"TUE",
"WED",
"THU",
"FRI"
]
}
关于java - JSON 错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51036530/