Skip to content

Instantly share code, notes, and snippets.

@rxtr007
Created March 16, 2019 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rxtr007/32d125089fe86fa6af52e87ba295c254 to your computer and use it in GitHub Desktop.
Save rxtr007/32d125089fe86fa6af52e87ba295c254 to your computer and use it in GitHub Desktop.
Convert Array of object into string and reverse.
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