简体   繁体   English

将JSON对象转换为Java类对象

[英]Convert JSON object into java class object

I have JSONObject having date field which I want to convert into Class object. 我有JSONObject具有要转换为Class对象的日期字段。

I am using Object Mapper() but while parsing I am getting error. 我正在使用Object Mapper(),但是在解析时出现错误。

JSONObject caseDetailsJson = method which returns JSONObject; JSONObject caseDetailsJson =返回JSONObject的方法;

ObjectMapper mapper = new ObjectMapper(); ObjectMapper映射器=新的ObjectMapper(); mapper.setDateFormat(apiDateFormat); mapper.setDateFormat(apiDateFormat);

While getting class object : ABC abc = mapper.readValue(caseDetailsJson.toJSONString(), ABC.class); 获取类对象时:ABC abc = mapper.readValue(caseDetailsJson.toJSONString(),ABC.class);

I am getting exception because of date parameter in JSON since date value does not has "" and it takes only starting word. 由于日期值中没有“”且仅使用起始字,因此由于JSON中的date参数而导致异常。

For example : Date value is :"dateCollected":Fri Jul 07 00:00:00 IST 1989, 例如:日期值为:“ dateCollected”:星期五1989年7月7日00:00:00 IST,

but its taking only Fri and not whole date. 但只花星期五而不是整个日期。

The date format does not appear to be standard. 日期格式似乎不是标准格式。

You use mapper.setDateFormat(apiDateFormat); 您使用mapper.setDateFormat(apiDateFormat); . Try using a proper date format that will match your string format. 尝试使用与您的字符串格式匹配的正确日期格式。

You did not provide enough details, and code samples would be welcomed for more accurate answers. 您没有提供足够的详细信息,欢迎提供代码示例以获取更准确的答案。

If your date value is not double quoted, it is not JSON as specified. 如果您的日期值未用双引号引起来,则它不是指定的JSON。 Have a look at here: https://tools.ietf.org/html/rfc7159 or here http://json.org/ 在这里看看: https : //tools.ietf.org/html/rfc7159或在这里http://json.org/

I'm afraid you have to tweak your input. 恐怕您必须调整输入内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM