swift - 如何解析这个JSON对象

时间:2015-02-08 00:48:39

标签: ios json swift

// Get the #1 app name from iTunes and SwiftyJSON
DataManager.getTopAppsDataFromItunesWithSuccess { (iTunesData) -> Void in
  let json = JSON(data: iTunesData)
    println(json)

如何访问["场地"] ["酒吧城市"] ["场地"]的所有元素?

{
    "venues":{
        "cityuser":"Beirut",
        "venue-usernewplace":{
            "star":[
            ],
            "idcat":[
            ],
            "namecat":[
            ],
            "name":[
            ],
            "id":[
            ],
            "phone":[
            ],
            "address":[
            ],
            "crossStreet":[
            ],
            "lat":[
            ],
            "lng":[
            ],
            "cc":[
            ]
        },
        "placesofpeople":{
            "star":"false",
            "nameplace":"B0 18",
            "idplace":"4b52670df964a520847b27e3",
            "count":"4",
            "cc":"LB",
            "phone":"01580018",
            "crossStreet":"Main Highway",
            "lat":"33.898404713314",
            "lng":"35.534128372291",
            "address":"Karantina"
        },
        "pubcity":{
            "venue":[
                {
                    "id":"4fe75b17e4b032d653ce50fd",
                    "idcat":"4bf58dd8d48988d11e941735",
                    "name":"Cl\u00e9 Cafe-Lounge Bar",
                    "phone":"71200712",
                    "address":"Mohammed Abdel Baki Street, Clemenceau",
                    "crossStreet":"Hamra, Facing Najjar Hospital",
                    "lat":"33.897185328966",
                    "lng":"35.487202808518",
                    "cc":"LB",
                    "count":"0",
                    "namecat":"Cocktail Bar",
                    "star":"false"
                },
                {
                    "id":"4e3e7533fa76455375c56a33",
                    "idcat":"4bf58dd8d48988d11f941735",
                    "name":"Skybar",
                    "phone":"03939191",
                    "address":"Biel",
                    "crossStreet":"Downtown Beirut",
                    "lat":"33.90610643966",
                    "lng":"35.510663636771",
                    "cc":"LB",
                    "count":"0",
                    "namecat":"Nightclub",
                    "star":"false"
                },
                {
                    "id":"4b52670df964a520847b27e3",
                    "idcat":"4bf58dd8d48988d11f941735",
                    "name":"B 018",
                    "phone":"01580018",
                    "address":"Karantina",
                    "crossStreet":"Main Highway",
                    "lat":"33.898404713314",
                    "lng":"35.534128372291",
                    "cc":"LB",
                    "count":"0",
                    "namecat":"Nightclub",
                    "star":"false"
                },

2 个答案:

答案 0 :(得分:0)

我知道有两种方法。

1将该文本墙发布到JSON格式化程序中,使该blob更具可读性。在这种情况下,您可以检查可以从中拉出的键。

2查看文档。

答案 1 :(得分:-1)

使用swiftyJson:

if let venues = json["venues"]["pubcity"]["venue"].array {
            //venue is an array of the dictionaries.
            for venue in venues {
                //just printing the name, but you have the whole dictioary of each venue here.
                println(venue["name"].string!)
            }
        }