简体   繁体   中英

optstring not converting to string [aws json object]

I am using com.amazonaws.util.json.JSONObject

In the documentation it says

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.

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?

First, the optString() method is supposed to return empty string: "" if there is no available String value. See the Documentation.

Second, the object {"time":1505900658464} has long value not string. You should use optLong instead. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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