1

I am trying to get Attendance Value from this json file "321" , the Date "654" and the value "123" from the "Number" in "@attributes". I have manage to filter through the levels but I was wondering if there is a more efficient way of accessing values in levels greater than three. Or is there a library similar to linq in csharp.

Can I just jump to "GameData" and get all the GameData and store to a list of objects.

or just a chain e.g:

json["level1"].["level2"].["level3"]

Json:

{
    "Feed":{
        "Doc":{
             "Comp": {
             },
             "GameData": {                  
                 "GameInfo": {
                     "Attendance": 321,
                      "Date": 654,
                      "ID": 00
                 }
             },      
             "GameData": {                  
                 "GameInfo": {
                     "Attendance": 321,
                     "Date": 654
                     "ID": 01
                 }          
             }
        },
        "@attributes": {
            "Number": 123
        }
    }
}

Code:

let json = try JSONSerialization.jsonObject(with: data!,      options: .mutableContainers) as! [String: AnyObject]
if let articlesFromJson = json["Feed"] as? [String: AnyObject]{
    print("level1")
        if let aFromJson = articlesFromJson["Doc"] as? [String: AnyObject]{
            print("level2")
                if let aFromJson = articlesFromJson["GameData"] as? [String: AnyObject]{
                    print(aFromJson)

        }
    }
}
Guig
  • 9,891
  • 7
  • 64
  • 126
Dev
  • 1,780
  • 3
  • 18
  • 46
  • Your json is weird: 1) there's a missing bracket after the first `GameData` 2) it doesn't make sense to have two entries for the same key `GameData`: the first one will be overwritten 3) it looks like you want numbers to be number and not string, so you should not use `"` ie `"321"` => `321` – Guig Dec 29 '16 at 22:27
  • Yes I know it doesn't make sense to have two of the same name it should be fine because of the unique ID? the json I am practicing with is not editable by me yes I know number should not be string I recreate the json and censored the data a bit – Dev Dec 29 '16 at 23:12
  • No as soon as you write two time the same key in a json, the first one will get overwritten. If you want to hold several items you've to use an array ie: `{"Doc": { "GamesData": [ { "ID": 1, ... }, { "ID": 1, ... } ] } }` – Guig Dec 30 '16 at 08:15

2 Answers2

1

I really like SwiftyJson that lets you do:

import SwiftyJSON
let json = JSON(data: data)
let game = json["Feed"]["Doc"]["GameData"] // this is of type JSON

let attendance = game["GameInfo"]["Attendance"].string ...

let number = json["Feed"]["@attributes"]["Number"].string // this is of type String?
let number = json["Feed"]["@attributes"]["Number"].stringValue // this is of type String, and will crash if the value is missing

=== EDIT: info on duplicated tags ===

JSON itself doesn't prevent you from doing so: https://stackoverflow.com/a/21833017/2054629 but you'll find that most librairies understand JSON as dictionaries and arrays, and those structures can't have duplicated keys.

For instance in javascript that you can test in your browser console:

JSON.stringify(JSON.parse('{"a": 1, "a": 2}')) === '{"a": 2}'

and I believe SwiftyJson behave similarly. SO I'd suggest you change your JSON structure to:

{
    "Feed":{
        "Doc":{
            "Comp": {},
            "GameData": {                  
                "GamesInfo": [
                    {
                        "Attendance": 321,
                        "Date": 654,
                        "ID": 0
                    },
                    {
                        "Attendance": 321,
                        "Date": 654
                        "ID": 1
                    }
                ]
            },
        },
        "@attributes": {
            "Number": 123
        }
    }
}
Community
  • 1
  • 1
Guig
  • 9,891
  • 7
  • 64
  • 126
  • works well... does it work with have duplicate tags this json file was poorly designed but i want to get around it if there is a away – Dev Jan 03 '17 at 23:17
0

Loop through the arrays until you are at the loop of the object you need. Then cast that value as needed.

mrabins
  • 197
  • 1
  • 2
  • 10