简体   繁体   English

optstring不转换为字符串[aws json对象]

[英]optstring not converting to string [aws json object]

I am using com.amazonaws.util.json.JSONObject 我正在使用com.amazonaws.util.json.JSONObject

In the documentation it says 在文档中说

public String optString(String key) 公共字符串optString(字符串键)

Get an optional string associated with a key. 获取与键关联的可选字符串。 It returns an empty string if there is no such key. 如果没有这样的键,它将返回一个空字符串。 If the value is not a string and is not null, then it is coverted to a string. 如果该值不是字符串且不为null,则将其覆盖为字符串。

Parameters: key - A key string. 参数:key-密钥字符串。 Returns: A string which is the value. 返回:一个字符串,它是值。

I have a json object like {"time":1505900658464} and when i use optstring i expect it to convert this long value to string ,but it actually returns " " (instead of converted long values as string).Am i missing something? 我有一个像{"time":1505900658464}这样的json对象,当我使用optstring时,我希望它将这个长值转换为字符串,但实际上返回“”(而不是将转换后的长值转换为字符串)。我缺少什么吗?

First, the optString() method is supposed to return empty string: "" if there is no available String value. 首先,如果没有可用的String值,则optString()方法应该返回空字符串:“”。 See the Documentation. 请参阅文档。

Second, the object {"time":1505900658464} has long value not string. 其次,对象{"time":1505900658464}具有长值而不是字符串。 You should use optLong instead. 您应该改用optLong See the Documentation . 请参阅文档

Also, you might need to read this discussion , and for converting time from Unix (that's the format you provided in your example) to simpleDateFormat see this. 另外,您可能需要阅读本讨论 ,并将时间从Unix(这是示例中提供的格式)转换为simpleDateFormat,请参见此内容

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

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