使用swiftjson循环对象

使用swiftjson循环对象,json,swift,Json,Swift,我使用SwiftJSON()在下面的json中循环: { "response": { "codes": [ { "id": "abc", "name": "Bob Johnson" }, { "id": "def", "name": "Benson"

我使用SwiftJSON()在下面的json中循环:

{
    "response": {
        "codes": [
            {
                "id": "abc",
                "name": "Bob Johnson"
            },
            {
                "id": "def",
                "name": "Benson"
            }
        ]
    }
}
我正在尝试循环通过
代码
块。到目前为止,我正在尝试:

let json = JSON(data: getJSON("<json_url>"))

    var people = json["response"]["codes"]

    let dataArray = nearBy.arrayValue!;

    println("Data items count: \(dataArray.count)")

    for item: AnyObject in dataArray {

        if let userName = item["name"].string{
            //Calm down, take it easy, the ".string" property still produces the correct Optional String type with safety
            println("Value" + userName)
        }

    }
如果这两种方法都能奏效,那将是有帮助的

{  "callout":{  "title":"Callout title","image":"http://image","url":"http://callouturl"},"categories":[  {  "category":"Category 1","articles":[  {  "title":"title 1","image":"image 1","url":"http://url1.com"},{ "title":"title 2","image":"image 2","url":"http://url2.com"}]},{  "category":"Category 2","articles":[  { "title":"title 3","image":"image 3","url":"http://url3.com"},{ "title":"title 4","image":"image 4","url":"http://url4.com"}]}]}
考虑到上面JSON的短处。我使用SwiftyJSON进行解析,如下所示:

let json:JSON = JSON(data:myData)

var catCollections:[CategoryCollection] = []

//gather category collections of articles
for (index: String, cat: JSON) in json["categories"] {

    //collect articles within each category
    var articles:[ArticleItem] = []
    for(index:String, art:JSON) in cat["articles"] {
        let artTitle = art["title"].string
        let artImage = art["image"].string
        let artUrl = art["url"].string

        if(artTitle != nil && artUrl != nil) {
            let articleItem = ArticleItem(title: artTitle!, url: artUrl!, imageURL: artImage)
            articles.append(ArticleItem)
        }

    }

    //create category collection for each category
    let catTitle = cat["category"].string ?? ""
    let catCollection = CategoryCollection(title: catTitle, articles: articles)
    catCollections.append(catCollection)
}

var callout:CalloutItem?
//check for existance of callout item
if let calloutTitle = json["callout"]["title"].string {
    if let calloutUrl = json["callout"]["url"].string {
        callout = CalloutItem(title: calloutTitle, url: calloutUrl, imageURL: json["callout"]["image"].string)
    }
}

下面是一个解决方案,我将使用它循环遍历JSON的“代码”,并获取(或打印)每个人的姓名

let = JSON(data : myData)

if let codes = json["response"]["codes"].array {
    for eachCode in codes {
        let name = eachCode["name"]
        print("Name: \(name)")
        // or do whatever you'd like with each 'name'
    }
}
这就是你所需要的。我不会为你用过的其他东西烦恼。我希望这能帮到你

let = JSON(data : myData)

if let codes = json["response"]["codes"].array {
    for eachCode in codes {
        let name = eachCode["name"]
        print("Name: \(name)")
        // or do whatever you'd like with each 'name'
    }
}