简体   繁体   中英

Try to parse JSON data with swift 3 using SwiftyJSON

I'm trying to parse some JSON-data that I got through an API call. I tried first the hard way: no libraries. When that didn't work, I tried SwiftyJSON, but that didn't help much either. I'm trying to get to all the sub-info from a beer.

Here you can see where I printed out the whole JSON 'object'.

Swift

let json = JSON(data: JSONData)
print(json);

Output

{
  "meta" : {
    "init_time" : {
      "measure" : "seconds",
      "time" : 0.011
    },
    "response_time" : {
      "measure" : "seconds",
      "time" : 0.056
    },
    "code" : 200
  },
  "notifications" : [

  ],
  "response" : {
    "macro" : {
      "count" : 10,
      "items" : [
        {
          "beer" : {
            "wish_list" : false,
            "beer_abv" : 5.4,
            "auth_rating" : 0,
            "style_name" : "Witbier",
            "beer_label" : "https:\/\/untappd.akamaized.net\/site\/beer_logos\/beer-3839_207a3_sm.jpeg",
            "bid" : 3839,
            "beer_name" : "Belgian White",
            "beer_style" : "Witbier"
          },
          "total_count" : 668,
          "your_count" : 0,
          "brewery" : {
            "location" : {
              "lng" : -104.992,
              "lat" : 39.7561,
              "brewery_city" : "Denver",
              "brewery_state" : "CO"
            },
            "brewery_name" : "Blue Moon Brewing Company",
            "brewery_slug" : "blue-moon-brewing-company",
            "brewery_id" : 127804,
            "country_name" : "United States",
            "brewery_label" : "https:\/\/untappd.akamaized.net\/site\/brewery_logos\/brewery-127804_1a670.jpeg",
            "contact" : {
              "twitter" : "BlueMoonBrewCo",
              "facebook" : "https:\/\/www.facebook.com\/bluemoon",
              "url" : "http:\/\/www.bluemoonbrewingcompany.com\/",
              "instagram" : ""
            },
            "brewery_active" : 1
          }
        },

Here I went specific on of the sub levels.

Swift

let json = JSON(data: JSONData)
let list = json["response","micro","items"]
print(list)

Output

[
  {
    "beer" : {
      "wish_list" : false,
      "beer_abv" : 4.7,
      "auth_rating" : 0,
      "style_name" : "IPA - Session \/ India Session Ale",
      "beer_label" : "https:\/\/untappd.akamaized.net\/site\/beer_logos\/beer-36834_82ca8_sm.jpeg",
      "bid" : 36834,
      "beer_name" : "All Day IPA",
      "beer_style" : "IPA - Session \/ India Session Ale"
    },
    "total_count" : 548,
    "your_count" : 0,
    "brewery" : {
      "location" : {
        "lng" : -85.6735,
        "lat" : 42.9585,
        "brewery_city" : "Grand Rapids",
        "brewery_state" : "MI"
      },
      "brewery_name" : "Founders Brewing Co.",
      "brewery_slug" : "founders-brewing-co",
      "brewery_id" : 549,
      "country_name" : "United States",
      "brewery_label" : "https:\/\/untappd.akamaized.net\/site\/brewery_logos\/brewery-foundersbrewingcompany_549.jpeg",
      "contact" : {
        "twitter" : "foundersbrewing",
        "facebook" : "http:\/\/www.facebook.com\/foundersbrewing",
        "url" : "http:\/\/www.foundersbrewing.com\/",
        "instagram" : "foundersbrewing"
      },
      "brewery_active" : 1
    }
  },

When I added "beer". I got a "null"-return. I think is't because of the difference in brackets. "[]" = list and "{}" = dictionaire. But I still couldn't solve it.

(I'm am not a iOS developer, just a student with his first interactions with swift3)

Swift

let json = JSON(data: JSONData)
let list = json["response","micro","items","beer"]
print(list)

Output

null

You can parse response object using SwiftyJSON like this. And your items object is array of beers. Hope you will get your idea.

let json = JSON(data: JSONData)
let list = json["response"]["micro"]["items"].arrayValue

for beerObjects in list {

       // You can get sub info of beer like this.
      let beer_name = beerObjects["beer"]["beer_name"].stringValue
      let total_count = beerObjects["total_count"].intValue
      let brewery_city = beerObjects["brewery"]["location"]["brewery_city"]

      // or parse information which you need
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM