1

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?

Balasubramanian
  • 700
  • 7
  • 26
Don Jose
  • 1,448
  • 2
  • 13
  • 27

1 Answers1

0

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.

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
TarekB
  • 757
  • 14
  • 20
  • then what does `If the value is not a string and is not null, then it is coverted to a string.` -means – Don Jose Jun 29 '18 at 06:26
  • it means if you are not sure that the key is always there or that its value is null or not a string..then it should give you empty string without crashing the app . – TarekB Jun 30 '18 at 02:46