UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

JSON Parsing {"git": [ Having problems in making it work

Forums > Swift

My Xcode file for help

Please look at my JSON on line #55 which I have commented out. My program works with Line # 54. All you need to do is Un-comment line #55 to switch out JSON'S What is fustrating is that I have been on this now for 8 days with not even coming close in to making it work. Thank you ahead of time for helping me out. robert

Line # 54 is identical except that line 55
{"git":[ { "login": "mojombo", "id": 1, } ] }

2      

Your two JSON files are not the same.

The one from line 54 has an array at the root:

[
  {
    "login": "mojombo",
    "id": 1,
    "node_id": "MDQ6VXNlcjE=",

while the one from line 55 has a "git" key:

{"git":[
  {
    "login": "mojombo",
    "id": 1,
    "node_id": "MDQ6VXNlcjE=",

The data struct you pass to JSONDecoder().decode(_:from:) needs to account for that difference.

You can do so this way:

  1. On line 82, change the property name to git instead of stories
  2. On line 105, use StoriesResponse.self instead of [JSONData].self
  3. On line 106, use completion(user.git) instead of completion(users)

3      

NOW I FINALLY GET IT, there was no way for me to finally understand the JSON response that the way you have pointed it to me. UNLESS I SPENT THE STRUGLING WEEK THAT I HAD. Thank you so much. I have finally found out my mistake. I am so appriciative for pointing out my 3 mistakes.

Sincerely robert

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.