java - optstring 未转换为字符串 [aws json 对象]

标签 java json string amazon-web-services

我正在使用com.amazonaws.util.json.JSONObject

在文档中它说

public String optString(String key)

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.

Parameters: key - A key string. Returns: A string which is the value.

我有一个像 {"time":1505900658464} 这样的 json 对象,当我使用 optstring 时,我希望它将这个 long 值转换为 string ,但它实际上返回“”(而不是转换为 string 的 long 值)。我错过了什么吗?

最佳答案

首先,如果没有可用的字符串值,optString() 方法应该返回空字符串:“”。 请参阅Documentation.

其次,对象{"time":1505900658464}具有长值而不是字符串。您应该使用 optLong 来代替。请参阅Documentation .

此外,您可能需要阅读此 discussion ,并将时间从 Unix (这是您在示例中提供的格式)转换为 simpleDateFormat 请参阅 this.

关于java - optstring 未转换为字符串 [aws json 对象],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46320838/

相关文章:

php - 使用 php 遍历不同的 DOM 节点和元素

java - 多线程环境中的库问题

java - 在 Spring Security OAuth2 中注入(inject)自定义 userDetailsS​​ervice 的问题

c# - 使用 LINQ 根据字段过滤 JSON 对象

.net - 如何通过反射检索字符串并将其升序连接

数组中的 PHP 最短/最长字符串长度

java - 如何解决 No bean named 'springSecurityFilterChain' available

java - Spring @Controller 和自定义 MethodNameResolver

c# - 使用 C# 和 JSON 在 ASP.Net 中填充下拉列表和文本框

javascript - 如何将 ScriptObjects 序列化为 JSON 以保存在 Silverlight 独立存储中?