2

I have working json data parsing array codes but I want to change it to Dictionary parsing. My new json file

{
  "Id": 450,
  "Name": "NameforItem",
  "Image": "234234.jpg"
}

My old json file working success

[{
  "Id": 450,
  "Name": "NameforItem",
  "Image": "234234.jpg"
}]

My parsing codes.

 /// Convert JSON data into  array
    private func getFromJSON(jsonData: NSData) throws -> [Place] {
        var places = [Place]()
        do {
            if let jsonArray = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments) as? [[String: AnyObject]] {
                for i in jsonArray {
                    var properties = [String: AnyObject]()
                    properties[placeJSONKeys.Id] = i[placeJSONKeys.Id]
                    properties[placeJSONKeys.Name] = i[placeJSONKeys.Name]
                    let place = Place(properties: properties)
                    places.append(place)
                }
            }
        } catch {
            throw TMDBErrors.ParsingError
        }
        return places
    }

Thank you !

0

2 Answers 2

2

I suggest you to change return type of your function to Optional:

private func getFromJSON(jsonData: NSData) throws -> Place? {
    var place: Place?
    do {
        if let json = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments) as? [String: AnyObject] {
            var properties = [String: AnyObject]()
            properties[placeJSONKeys.Id] = json[placeJSONKeys.Id]
            properties[placeJSONKeys.Name] = json[placeJSONKeys.Name]
            place = Place(properties: properties)
        }
    } catch {
        throw TMDBErrors.ParsingError
    }
    return place
}

updated

 private func getFromJSON(jsonData: NSData) throws -> Place? {
    var place: Place?
    do {
        if let json = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments) as? [String: AnyObject] {


            place = Place(properties: json)
        }
    } catch {
        throw TMDBErrors.ParsingError
    }
    return place
}
Sign up to request clarification or add additional context in comments.

6 Comments

@Shadow Of Worked Ty I will approve it soon
@SwiftDeveloper - one question , check one the updated answer also
@Anbu.Karthik Thank you
@SwiftDeveloper - the reason placeJSONKeys is the struct class , you can directly access the objcet in anywhere
@Anbu.Karthik yeah i understand Thank you !
|
1

Change your [[String: AnyObject]] with [String: AnyObject] because now it is dictionary not array

do {
    if let jsonDic = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments) as? [String: AnyObject] {
        print(jsonDic["Id"])
        print(jsonDic["Name"])
        print(jsonDic["Image"])
    }
} catch {
    throw TMDBErrors.ParsingError
}

1 Comment

why you are increase the memory you can directly access the JSonarray dictionary no need of loop in here bro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.