I have a return JSON in my application in Swift, and have a field that returns me a date. When I refer to this data, the code gives me something like "/ Date (1420420409680) /". How do I convert this into NSDate? In Swift, please, I´ve tested examples with Objective-C, without success.
相关问题
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Correctly parse PDF paragraphs with Python
- Core Data lightweight migration crashes after App
相关文章
- 现在使用swift开发ios应用好还是swift?
- json_encode 没有把数组转为json
- Livy Server: return a dataframe as JSON?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Unexpected end of JSON input from an ajax call
- Where does a host app handle NSExtensionContext#co
That looks very similar to the JSON encoding for a date as used by Microsoft's ASP.NET AJAX, which is described in An Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET:
The only difference is that you have the format
/Date(ticks)/
and not\/Date(ticks)\/
.You have to extract the number between the parentheses. Dividing that by 1000 gives the number in seconds since 1 January 1970.
The following code shows how that could be done. It is implemented as a "failable convenience initializer" for
NSDate
:Example usage (with your date string):
This gives the output
Update for Swift 3 (Xcode 8):
Example:
It looks like an UNIX Timestamp: 01/12/2015 @ 6:14pm (UTC) [According to http://www.unixtimestamp.com/index.php ]
You can convert it to an NSDate object using the constructor NSDate(timeIntervalSince1970: unixTimestamp)
Adding onto what the others have provided, simply create utility method in your class below:
Then you can call this method in your successfully returned, parsed JSON object, like below:
Or you can ignore the utility method and the caller code above entirely and simply do this:
And that should work!