gpt4 book ai didi

Java JSON 解码器

转载 作者:太空宇宙 更新时间:2023-11-04 13:21:53 28 4
gpt4 key购买 nike

我收到错误代码(如下),并且无法解码该字符串,我知道它正在查找 JSON 和我也保存的文件,但它无法解析。错误代码:

Unexpected token VALUE(-10) at position 83.
at org.json.simple.parser.JSONParser.parse(JSONParser.java:257)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
at Employee.retrieveData(Employee.java:103)
at EmployeeTest.main(EmployeeTest.java:9)

代码:

JSONParser parser = new JSONParser();
String s = null;
try {

if (!file.exists()) {
System.out.println("Error: No Data");
return;
}

FileReader fr = new FileReader(file.getAbsoluteFile());
BufferedReader br = new BufferedReader(fr);
s=br.readLine();
fr.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}

try {
//s = {"firstName":"Bob","lastName":"Hope","isEmployeeClockedIn":false,"clockedOut":2015-10-06 23:51:10.751,"id":0,"clockedIn":2015-10-06 23:51:10.751}
Object obj = parser.parse(s);
JSONArray array = (JSONArray)obj;
System.out.println(obj);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

最佳答案

这不是有效的 JSON:

{"firstName":"Bob","lastName":"Hope","isEmployeeClockedIn":false,"clockedOut":2015-10-06 23:51:10.751,"id":0,"clockedIn":2015-10-06 23:51:10.751}

解析在“clockedOut”处失败:2015 -10 -06。

该值应加引号:

"clockedOut":"2015-10-06 23:51:10.751"

关于Java JSON 解码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32983642/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com