Created
March 16, 2019 06:15
-
-
Save rxtr007/32d125089fe86fa6af52e87ba295c254 to your computer and use it in GitHub Desktop.
Convert Array of object into string and reverse.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let value1 = [ | |
"aboutme" : "", | |
"active" : 0, | |
"birthday" : "1992-10-14", | |
"city_id" : 0, | |
"email" : "test@test.com", | |
"fbid" : "", | |
"firstname" : "test" | |
] as [String : Any] | |
let value2 = [ | |
"aboutme" : "1", | |
"active" : 1, | |
"birthday" : "11", | |
"city_id" : 1, | |
"email" : "1test@test.com", | |
"fbid" : "1", | |
"firstname" : "1test" | |
] as [String : Any] | |
var jsonArray = NSMutableArray() | |
jsonArray.add(value1) | |
jsonArray.add(value2) | |
let json = jsonArray//[value1,value2] | |
var str = "" | |
do { | |
let jsonData = try JSONSerialization.data(withJSONObject: json, options: []) | |
str = String(data: jsonData, encoding: .utf8)! | |
print(str) | |
}catch { | |
print("error") | |
} | |
let jsonObjData = str.data(using: String.Encoding.utf8) | |
do { | |
if let arr = try JSONSerialization.jsonObject(with: jsonObjData!, options: .allowFragments) as? NSArray { | |
print("Array : \(arr)") | |
}else { | |
print("bad json") | |
} | |
}catch { | |
print("erroror") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment