SwiftyJSON looping through an array of JSON objects
Asked Answered
A

4

11
[
    {
        "cont": 9714494770,
        "id": "1",
        "name": "Kakkad"
    },
    {
        "cont": 9714494770,
        "id": "2",
        "name": "Ashish"
    }
]

The one above is a json array filled with JSON objects. I don't know how to parse through this with SwiftyJSON

Annadiana answered 20/4, 2015 at 20:15 Comment(2)
JSON does not use typographical quotes “ ... ” :)Liscomb
Just assume they are normal quotationsAnnadiana
B
12

Example from the SwiftyJSON page, adapted to your data:

let json = JSON(data: dataFromNetworking)
for (index, object) in json {
    let name = object["name"].stringValue
    println(name)
}
Buxtehude answered 20/4, 2015 at 20:27 Comment(2)
Why can't it just be for object in json? What does for (index, object) in json do differently that makes it work exactly? I've never seen that before. ThanksEra
You can use _ instead of index, but the index can be helpful for certain situations when you need to know where you are in the array. Or you can use key, object for dictionaries.Rebozo
B
3

Assuming [{"id":"1", "name":"Kakkad", "cont":"9714494770"},{"id":"2", "name":"Ashish", "cont":"9714494770"}] is assigned to a property named jsonData.

let sampleJSON = JSON(data: jsonData)

let sampleArray = sampleJSON.array sampleArray is an optional array of JSON objects.

let firstDict = sampleArray[0] firstDict is an optional JSON dict.

let name = firstDict["name"] is an optional JSON object

let virtName = name.string is a optional string (In this case "Kakkad").

let realName = name.stringValue realName is a string or an empty string.

You could also use: let longName = sampleJSON[0]["name"].stringValue

After you initialize the JSON object with data all of the elements are JSON types until you convert them to a swift type.

  • .string optional (string or null)
  • .stringValue string or "" empty string
  • .dict optional ([String: AnyObject] or null)
  • .dictValue ([String: AnyObject] or String: AnyObject)
Brancusi answered 20/4, 2015 at 20:56 Comment(0)
L
0

For Swift4 I have updated the code from Moritz answer

    if let path : String = Bundle.main.path(forResource: "tiles", ofType: "json") {
                if let data = NSData(contentsOfFile: path) {
                    let optData = try? JSON(data: data as Data)
                    guard let json = optData else {
                        return
                    }
//If it is a JSON array of objects
                    for (_, object) in json {
                        let name = object["name"].stringValue
                        print(name)
                    }
                }
            }
Laundes answered 26/3, 2018 at 0:33 Comment(0)
A
0

Swift 3 or 4 code like this:

let json = JSON(yourData)
for (_, object) in json {
    let cont = object["cont"].stringValue
    print(cont)
}

You can put index instead of _ if you use is anywhere in your code. If you don't use a variable, it's better to put _ (XCode also gives warnings).

Aronow answered 2/8, 2018 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.