0

Having trouble which is probably so minor but my searches turn up nothing. I have a json model as follows :

//quick syntax to give you an idea of the model

{user:
  {"name": "",
   "password": "",
   "medium" : {
      {"title":"",
      {"description":""}}}

I'm getting the above data model from a GET request to user and it returns all the info above but when i try to parse the "medium" information such as "title" & "description" I'm having no luck. I get these responses in Xcode that say

"Value of object 'AnyObject' not unwrapped, did you mean to use ! or ?"

and then when i click on the round red Xcode message to fix it it places !s and ?s everywhere but the error remains. Here is my parse method which worked perfectly fine when I was parsing only from the "medium". Any idea what I'm doing wrong?

a portion of the parse method where i get the same error for each attribute: all lines with the same error indicated by *

// Parse JSON data
            let jsonMedium = jsonResult?["user"] as! [AnyObject] //media where user is
            for jsonMedia in jsonMedium {
                let media = Media()
                *media.title = jsonMedia["medium"]["title"] as! String
                *media.description = jsonMedia["medium"]["description"] as! String
                *media.category = jsonMedia["medium"]["category"] as! String
                *media.image = jsonMedia["medium"]["image"] as! String

                *if let IDer = jsonMedia["medium"]["id"] as? Int{
                        var stringIder = String(IDer)
                        media.identifierString = stringIder

                }

Still no luck with anything. I don't understand why it works with regular JSON but Xcode won't accept anything when I try to obtain nested. All of your help has been appreciated. In the meantime here's the full method if it helps any further

func parseJsonData(data: NSData) -> [Media] {
        var medium = [Media]()
        do {
            let jsonResult = try NSJSONSerialization.JSONObjectWithData(data,
                                                                        options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
            // Parse JSON data
            let jsonMedium = jsonResult?["media"] as! [AnyObject]
            for jsonMedia in jsonMedium {
                let media = Media()
                media.title = jsonMedia["medium"]["title"] as! String
                media.description = jsonMedia["medium"]["description"] as! String
                media.category = jsonMedia["medium"]["category"] as! String
                media.image = jsonMedia["medium"]["image"] as! String

                if let IDer = jsonMedia["medium"]["id"] as? Int{
                        var stringIder = String(IDer)
                        media.identifierString = stringIder

                }


                medium.append(media)
            }
        } catch {
            print(error)
        }
        return medium
    }
5
  • You are getting a syntax error on xcode?? Commented May 6, 2016 at 15:29
  • Yes. The value of object one I described for all of the lines listed with a * Commented May 6, 2016 at 15:31
  • can you come on skype?? Commented May 6, 2016 at 15:32
  • Occasionally I have to Product->Clean after fixing code for Xcode to remove error messages. Commented May 6, 2016 at 15:40
  • I just tried your code and their is no syntax error jsonResult is AnyObject right?? did u miss the last bracket?? "}" Commented May 6, 2016 at 15:40

3 Answers 3

1
let json = [
    "user" : [
        "name" : "My Name",
        "password" : "My Password",
        "medium" : [
            "title" : "My Title",
            "description" : "My Description"
        ]
    ]
]

if let userJson = json["user"] as? [String : AnyObject] {
    if let name = userJson["name"] as? String {
        print("name: \(name)")
    }
    if let password = userJson["password"] as? String {
        print("password: \(password)")
    }
    if let mediumJson = userJson["medium"] as? [String : AnyObject] {
        if let title = mediumJson["title"] as? String {
            print("title: \(title)")
        }
        if let description = mediumJson["description"] as? String {
            print("description: \(description)")
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe it helps

let request : ASIFormDataRequest = ...your request

        if request.responseString() != nil {
            var jsonResponse : Dictionary<String, AnyObject>?
            do{
                jsonResponse = try NSJSONSerialization.JSONObjectWithData(request.responseData(), options: NSJSONReadingOptions.AllowFragments) as? Dictionary<String, AnyObject>

            } catch _ {
                //some error
            }
        }

Comments

0

FIXED IT! Took an entire day of deep thought and google/youtube/stack/brainstorming and your help but it was one changed line that got the whole thing going

// Parse JSON data
            let jsonMedium = jsonResult?["user"]!["medium"] as? [AnyObject]
            for jsonMedia in jsonMedium! {
                let media = Media()
                media.title = jsonMedia["title"] as! String
                media.description = jsonMedia["description"] as! String

instead of :

let jsonMedium = jsonResult?["user"] as! [AnyObject] //media where user is
            for jsonMedia in jsonMedium {
                let media = Media()
                *media.title = jsonMedia["medium"]["title"] as! String
                *media.description = jsonMedia["medium"]["description"] as! String

Comments

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.