Parse Nested Array in JSON in Swift - ios

I'm trying to Parse JSON with code and structure like this:
userApiService.getAllUsers { (responseDict:NSDictionary?, error:NSError?) -> Void in
//Parse responseDict for the key "result"
}
Here is Json Structure
{
error = "";
result = (
{
name = AnotherUser;
password = AnotherPassword;
userId = 1343;
},
{
name = TestUser;
password = TestPassword;
userId = 1344;
},
{
name = TestUser;
password = TestPassword;
userId = 1347;
},
);
status = 200;
}
I've tried code like this:
self.loadingIcon.endRefreshing()
if let resultDict = responseDict["result"] as? NSArray {
for userRecord in resultDict{
var userModel = User(userDict: userRecord as! NSDictionary)
self.tableData.append(userModel)
}
}
self.tblView.reloadData()
}
But this results in the error "NSArray?" is not convertible to StringLiteralConvertible If I remove the optional and add the ! force unwrap to the closure signature then this error goes away. However, I've seen instances where my app was crashing if something wrong came from the backend. So my questions are:
Is there a way to parse this JSON and still keep the optional NSDictionary in the closure signature.
Or do I just need to check if the dictionary is not nil and then proceed with the code I posted above?

You can use "nil coalescing" to access the key in the Optional dictionary by adding a ? between the dictionary variable and its subscript, like this:
if let resultDict = responseDict?["result"] as? NSArray {
// ...
}
With this syntax the evaluation will not try to access the key if responseDict is nil.

The easiest way to do this is to use a library.
1) You can use swiftyJSON. It uses the objective C JSON parsing library. https://github.com/SwiftyJSON/SwiftyJSON
2) If you want a library which uses a pure swift parser try JSONSwift. The readme on github shows how you can retrieve nested values from the JSON file. And integrating it in your project just requires you to import a single file. https://github.com/geekskool/JSONSwift

Try to use objectForKey to retrive the data from the dictionary, like so:
self.loadingIcon.endRefreshing()
if let resultDict = responseDict.objectForKey("result") as? NSArray {
for userRecord in resultDict{
var userModel = User(userDict: userRecord as! NSDictionary)
self.tableData.append(userModel)
}
}
self.tblView.reloadData()
}

Related

How to map a json that I don't know the number of the parameters

Hello I have a json the returns me some parameters as variables.
It has Parameter1, Parameter2, Parameter3 etc..
I don't know how many parameters will it give me. It's not a list it's just different variables in the json.
Which is the best way to map a json like that? I use Object Mapper
For Example:
First Time the json is
{
"MyObject": {
"Parameter1": "p1",
"Parameter2": "p2",
"Parameter3": "p3",
"Parameter4": "p4"
}
}
And a second time the json is
{
"MyObject": {
"Parameter1": "p1",
"Parameter2": "p2",
"Parameter3": "p3"
}
}
You can try this.
let keyvalue = parentDict.value(forKey: "MyObject") as! NSDictionary
var lastValue = Keyvalue.allValues
var lastKey = Keyvalue.allKeys
for Keyname in Keyvalue.allKeys
{
print("Keyname %#",Keyname)
print("Value %#",Keyvalue.value(forKey:Keyname))
}
the first step to parse any JSON to make it reusable is to create your Model class or struct accordingly.
Create a class called MyObject as same as your json dictionary
Create a let/var property parameters: [String]?. It's optional as API isn't reliable and maybe wont send anything at all.
See the example below how I parse the json object below.
class MyObject {
let parameters: [String]?
// it's failable because maybe json different at runtime & couldn't parse
init?(json: [String:AnyObject]) {
var key = "Parameter"
var parms = [String]()
for i in 0..<json.count {
guard let item = json["\(key)\(i+1)"] as? String else { continue }
params.append(item)
}
self.parameters = params
}
}
Now you can access the parameters array with index.
Well this could be refactored and you can get the idea how you will handle this with that library.

iOS 9 JSON Parsing loop

I'm creating an app that should retrieve some JSON from a database.
This is how my JSON looks:
[{"id":"1","longitude":"10","latitude":"10","visibility":"5","timestampAdded":"2015-10-01 15:01:39"},{"id":"2","longitude":"15","latitude":"15","visibility":"5","timestampAdded":"2015-10-01 15:06:25"}]
And this is the code i use:
if let jsonResult = JSON as? Array<Dictionary<String,String>> {
let longitudeValue = jsonResult[0]["longitude"]
let latitudeValue = jsonResult[0]["latitude"]
let visibilityValue = jsonResult[0]["visibility"]
print(longitudeValue!)
print(latitudeValue!)
print(visibilityValue!)
}
As you can see it only gets the first chunk from the JSON and if there are no JSON at all it will crash, but if i want it to count the amount and make an array out of it like this:
var longitudeArray = [10, 15]
var latitudeArray = [10, 15]
And so on...
I also need this to be apple watch compatible so i can't use SwiftyJSON.
What do i do? I really hope you can help me!
Thanks.
SOLVED!
Problems was solved by "Eric D."
This is the code:
do {
if let url = NSURL(string: "YOU URL HERE"),
let data = NSData(contentsOfURL: url),
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [[String:AnyObject]] {
print(jsonResult)
let longitudeArray = jsonResult.flatMap { $0["longitude"] as? String }
let latitudeArray = jsonResult.flatMap { $0["latitude"] as? String }
print(longitudeArray)
print(latitudeArray)
}
} catch let error as NSError {
print(error.description)
}
Thank you soo much Eric!! :-)
You could use flatMap to get an array of your elements:
let longitudeArray = jsonResult.flatMap { $0["longitude"] as? String }
let latitudeArray = jsonResult.flatMap { $0["latitude"] as? String }
etc.
flatMap is like map but unwraps optionals, which is adequate because we need to safely cast the type of the object we get from each dictionary in the json array.
$0 represents the object in the current iteration of flatMap of the array it's applied to.
If you're currently using SwiftyJSON, then that would be:
let longitudeArray = jsonResult.flatMap { $1["longitude"].string }
let latitudeArray = jsonResult.flatMap { $1["latitude"].string }
because .string is SwiftyJSON's optional String value getter.
But as you said, you don't want to use it (anymore), so you need to use NSJSONSerialization to decode your JSON data, there's plenty of examples on the Web and on SO. Then you will be able to use my original answer.
You're already getting an array with all of the elements (not just the first one. you're simply only accessing the first one). jsonResult is an array of dictionaries. Each dictionary (in this case, based on the json you provided) contains these elements: id, longitude, latitude, visibility and timestampAdded. In order to access each of them, you can simply loop over jsonResult and access the i'th element (and not always the 0 element). This will also prevent the crash you're experiencing with the json is blank or invalid (since you'll only be going over the valid elements in jsonResult.
This will give you the flexibility to create the custom arrays you wish to create (in order to create an array of all of the longitudes, for example, you will simply add that element to the new array while looping over jsonResult). However, if you'd like to save yourself the trouble of manually building these arrays and assuming you have control over the json structure, I would recommend changing the received json to the relevant structure (a dictionary or arrays instead of an array of dictionaries), so it would better fit your needs and provide you the results in the relevant format right "out of the box".

iOS, Swift - JSON Response. VK SDK

I am using the VK SDK for iOS, and I need to take out the images URLs from JSON response from VK.
The response is look like this:
The actual response could be AnyObject OR String, and I only need the largest image URL (photo_1280) as string.
In the response comes 1-10 photos and which parameters I will need to change to get the particular photo like first or second and so on.
I'm using Swift in my project but can understand Objective-C.
JSON is just a format that lets you exchange information between languages (or objects).
You need to 'parse' the string into a JSON object. Its a little different for each language. For example on iOS I create my UI elements from a JSON file where I load the file and create a dictionary object from it. In your case you are doing it from a string. You need to turn that string into a valid object in your language. In Swift I prefer a Dictionary. So I use a typealias for that and cast the nsDictionary as that type of object.
Then to access objects, I access keys in the dictionary. In your case you would create a dictionary object too, and access the "attachments" object, which in your case is an array so you'll need to do extra processing on it to get each image (i.e. go through the array). This should get you going.
typealias Dict = Dictionary<String,AnyObject>
func loadDictionaryFromJSON(jsonString:String) -> Dict
{
var JSONData:NSData! = jsonString.dataUsingEncoding(NSUTF8StringEncoding)
var JSONError:NSError?
let swiftObject:AnyObject = NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions.AllowFragments, error: &JSONError)!
if let nsDictionaryObject = swiftObject as? NSDictionary
{
if let dictionaryObject = nsDictionaryObject as Dictionary?
{
return dictionaryObject as Dict
}else
{
println("Error could not make dictionary from NSDictionary in \(self)")
}
}else
{
"Error could not make NSDictionary in \(self)"
}
println("Empty dictionary passed, fix it!")
return Dict()
}
Now to access things you just do
var objects:Dictionary<String,AnyObject> = loadDictionaryFromJSON("{"what":"ever"}")
var whatever = objects["what"]

Cannot downcast from 'AnyObject' whilst parsing JSON array

I'm in the process of trying to migrate an existing project to Swift, its more of a learning exercise but I think I've hit a problem which seems pretty basic and I think it may relate to my lack of understanding of AnyObject.
I've created a object that consists of a number of variables and is initialised as:
var customObject:MycustomObject = MYcustomObject()
I'm then using NSURLConnection to retrieve JSON data and this all appears to be working correctly as the jsonArray is being populated with data
jsonArray = NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSArray
I'm then looping through the array and attempting to parse the data and this is where I'm experiencing problems, the for loop defaults details to AnyObject.
for details:AnyObject in jsonArray {
parseDetail(details as NSDictionary)
}
The problem relates to the integerValue, its generating an "Cannot downcast from 'AnyObject' to non-#objc...." error
func parseDetail(element: NSDictionary ) {
self.customObject.stringValue = element["id"] as String
self.customObject.integerValue = element["type"] as Integer
}
There also seems to be some differences in the way NSString & String are working, my understanding so far is that when using Swift I should be using the native types Sting, Float Integer etc etc. If I use NSString the stringValue displays correctly in the debug window but when using the type String I receive the following:
{
core = {
_baseAddress = Builtin.RawPointer = 0x0b227f49
_countAndFlags = 1073741828
_owner = Some {
Some = (instance_type = Builtin.RawPointer = 0x0b227f40 -> 0x006d58f0 (void *)0x006d58c8: __NSCFString)
}
}
}
Sorry this is a little long but is there any info regarding threading, this is my next challenge?
NSThread.detachNewThreadSelector(Selector: parseDetail(), toTarget: self, withObject: nil)
Here's a little example that does the job. First, here's a model class for our purposes:
class CustomObject {
var name: String = ""
var age : Int = 0
init(json: Dictionary<String, AnyObject>) {
name = json["name"] as NSString
age = (json["age" ] as NSNumber).integerValue
}
}
As you can see, we still need to deal with NSNumbers and NSStrings since NSJSONSerialization is not yet updated to the Swift types.
Next, a little json.txt file residing in our project:
[{"name":"Alice","age":30},{"name":"Bob","age":40}]
And last, the parser, right in application launch for a quick test:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
var filePath = NSBundle.mainBundle().pathForResource("json", ofType:"txt")
var data = NSData(contentsOfFile:filePath)
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: nil) as Array<Dictionary<String, AnyObject>>
var customObjects = json.map { dict in CustomObject(json: dict) }
println(customObjects[0].name)
println(customObjects[0].age)
return true
}
This will print:
Alice
30
A progress update, the following items are correct and are working:
.interegervalue
.doublevalue
.boolvalue
The reason why the above were appearing not to be working is that the JSON data had changed and some elements no longer existed. So element["type"] didn't exist, using Swift this results in a crash whereas in Objective-C no crash occurs.
All part of the learning process..

How do I parse an array inside parsed JSON in Swift?

I'm using an API that returns JSON that looks like this
{
"boards":[
{
"attribute":"value1"
},
{
"attribute":"value2"
},
{
"attribute":"value3",
},
{
"attribute":"value4",
},
{
"attribute":"value5",
},
{
"attribute":"value6",
}
]
}
In Swift I use two functions to get and then parse the JSON
func getJSON(urlToRequest: String) -> NSData{
return NSData(contentsOfURL: NSURL(string: urlToRequest))
}
func parseJSON(inputData: NSData) -> NSDictionary{
var error: NSError?
var boardsDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary
return boardsDictionary
}
and then I call it using
var parsedJSON = parseJSON(getJSON("link-to-API"))
The JSON is parsed fine. When I print out
println(parsedJSON["boards"])
I get all the contents of the array. However I am unable to access each individual index. I'm positive it IS an Array, because ween I do
parsedJSON["boards"].count
the correct length is returned. However if I attempt to access the individual indices by using
parsedJSON["boards"][0]
XCode turns off syntax highlighting and gives me this:
and the code won't compile.
Is this a bug with XCode 6, or am I doing something wrong?
Dictionary access in Swift returns an Optional, so you need to force the value (or use the if let syntax) to use it.
This works:
parsedJSON["boards"]![0]
(It probably shouldn't crash Xcode, though)
Take a look here:https://github.com/lingoer/SwiftyJSON
let json = JSONValue(dataFromNetworking)
if let userName = json[0]["user"]["name"].string{
//Now you got your value
}
You can create a variable
var myBoard: NSArray = parsedJSON["boards"] as! NSArray
and then you can access whatever you have in "boards" like-
println(myBoard[0])
The correct way to deal with this would be to check the return from the dictionary key:
if let element = parsedJSON["boards"] {
println(element[0])
}

Resources