1
struct APOD: Codable {
    let points: String
    let full_name: String
    let description: String
}

let decoder = JSONDecoder()
let product = try! decoder.decode(APOD.self, from: jsonData.data(using: .utf8)!)

print(product.full_name)

I have a String called jsonData that comes from: https://www.instagram.com/georgeanisimow/?__a=1. I formatted the file and pasted it into the project just to have something work.

Unfortunately it fails with this error code:

"Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "points", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"points\", intValue: nil) (\"points\").", underlyingError: nil))"

I am trying to print the value for "full_name" in the JSON.

Here is the begginng of the JSON:

let jsonData ="""    
{  
   "logging_page_id":"profilePage_592027119",
   "show_suggested_profiles":false,
   "graphql":{  
      "user":{  
         "biography":"- Represented by AEFH Talent and CESD Modeling - I travel a lot -",
         "blocked_by_viewer":false,
         "country_block":false,
         "external_url":null,
         "external_url_linkshimmed":null,
         "edge_followed_by":{  
            "count":4571
         },
         "followed_by_viewer":true,
         "edge_follow":{  
            "count":741
         },
         "follows_viewer":true,
         "full_name":"George Anisimow"
      }
   }
}"""
5
  • The error is pretty clear: There is no key points in the root object of the JSON. The link doesn't show anything. Commented Jan 5, 2019 at 11:53
  • @vadian Sorry, didn't realize that, Either way, I have the JSON pasted into the project, no fetching data. Commented Jan 5, 2019 at 12:01
  • 1
    Please edit the question and add the (beginning of the) JSON. Commented Jan 5, 2019 at 12:03
  • @vadian Did so just now Commented Jan 5, 2019 at 12:11
  • Include points, full_name, description keys in the JSON you have added in the question. Commented Jan 5, 2019 at 12:16

2 Answers 2

1

You get full_name with these structs ( I specified only the relevant keys)

struct Root: Decodable {
    let graphql : Graphql
}

struct Graphql: Decodable {
    let user : User
}

struct User: Decodable {
    let fullName : String
}

and decode the data

let data = Data(jsonData.utf8)
do {
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    let result = try decoder.decode(Root.self, from: data)
    let fullname = result.graphql.user.fullName
    print(fullname)

} catch { print(error) }
Sign up to request clarification or add additional context in comments.

Comments

0

There are many ways of doing this

First-Method:

do {
    let responseData = Data(data.utf8)
    let decodeData = try JSONDecoder().decode(Controller.self, from: responseData)

    if (decodeData.ErrorCode! == "0") {
        //Success
    } else {
        //Failure
    }
} catch let jsonErr {
    //Failure
}

Second-Method:

do {
    if let responseData = response.data, let decodedData = try JSONSerialization.jsonObject(with: responseData, options: []) as? [[String: Any]] {
     print(decodedData)
    }
} catch let error as NSError {
    print(error)
}

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.