使用swift迭代字典json数据时出错:找不到接受提供的参数的'subscript'的重载

时间:2014-06-30 13:38:53

标签: json dictionary swift

我想出了如何在我的项目中读取本地json文件。但现在我无法迭代它。这是我的原始json数据:

{
    "locations": [
                  {
                  "title": "The Pump Room",
                  "place": "Bath",
                  "latitude": 51.38131,
                  "longitude": -2.35959,
                  "information": "The Pump Room Restaurant in Bath is one of the city’s most elegant places to enjoy stylish, Modern-British cuisine.",
                  "telephone": "+44 (0)1225 444477",
                  "url": "http://www.romanbaths.co.uk",
                  "visited" : true
                  },
                  {
                  "title": "The Eye",
                  "place": "London",
                  "latitude": 51.502866,
                  "longitude": -0.119483,
                  "information": "At 135m, the London Eye is the world’s largest cantilevered observation wheel. It was designed by Marks Barfield Architects and launched in 2000.",
                  "telephone": "+44 (0)8717 813000",
                  "url": "http://www.londoneye.com",
                  "visited" : false
                  },
                  {
                  "title": "Chalice Well",
                  "place": "Glastonbury",
                  "latitude": 51.143669,
                  "longitude": -2.706782,
                  "information": "Chalice Well is one of Britain's most ancient wells, nestling in the Vale of Avalon between the famous Glastonbury Tor and Chalice Hill.",
                  "telephone": "+44 (0)1458 831154",
                  "url": "http://www.chalicewell.org.uk",
                  "visited" : true
                  },
                  {
                  "title": "Tate Modern",
                  "place": "London",
                  "latitude": 51.507774,
                  "longitude": -0.099446,
                  "information": "Tate Modern is a modern art gallery located in London. It is Britain's national gallery of international modern art and forms part of the Tate group.",
                  "telephone": "+44 (0)20 7887 8888",
                  "url": "http://www.tate.org.uk",
                  "visited" : true
                  },
                  {
                  "title": "Eiffel Tower",
                  "place": "Paris",
                  "latitude": 48.858271,
                  "longitude": 2.294114,
                  "information": "The Eiffel Tower (French: La Tour Eiffel, is an iron lattice tower located on the Champ de Mars in Paris.",
                  "telephone": "+33 892 70 12 39",
                  "url": "http://www.tour-eiffel.fr",
                  "visited" : false
                  },
                  {
                  "title": "Parc Guell",
                  "place": "Barcelona",
                  "latitude": 41.414483,
                  "longitude": 2.152579,
                  "information": "Parc Guell is a garden complex with architectural elements situated on the hill of El Carmel in the Gràcia district of Barcelona.",
                  "telephone": "+34 902 20 03 02",
                  "url": "http://www.parkguell.es",
                  "visited" : false
                  }
                  ]
}

这就是我阅读和解析它的方式:

  func readLocationData() {

        let bundle = NSBundle.mainBundle()
        let path = bundle.pathForResource("locations", ofType: "json")
        let content : NSString = NSString.stringWithContentsOfFile(path) as NSString

        let locationData:NSData = content.dataUsingEncoding(NSUTF8StringEncoding)

        var error: NSError?

        var jsonDict:Dictionary = NSJSONSerialization.JSONObjectWithData(locationData, options: nil, error: &error) as Dictionary<String, AnyObject>

        if let locations : AnyObject = jsonDict["locations"] {
            if let information: String = locations["information"] {
               // I get error here: Could not find an overload for 'subscript' that accepts the supplied arguments
            }
        }

    }

现在我如何访问titleplacelocation等内部密钥?我在循环中尝试的任何内容都会产生类似的错误:

Could not find an overload for 'subscript' that accepts the supplied arguments

2 个答案:

答案 0 :(得分:0)

让我们看看JSON的不同部分是什么类型:jsonDict是一个字典,其数组是唯一的值,而jsonDict["locations"]是其他字典的数组。您应该Dictionary<String, Any>使用jsonDict,然后您将获得更好的运气:

var jsonDict:Dictionary = NSJSONSerialization.JSONObjectWithData(locationData, options: nil, error: &error) as Dictionary<String, Any>
let locations: NSArray? = jsonDict["locations"] as? NSArray

if locations {
    for location: AnyObject in locations! {
        if let information = location as? NSDictionary {
            println(information)
        }
    }
}

答案 1 :(得分:0)

你有一个字典,其中包含一个“locations”条目,这是一个字典数组。

试试这个:

// I'm using NSDictionary since it is easier to work with when you don't know the object // // types  but you can convert it later with explicit data types

if let results = jsonDict["locations"] as NSDictionary[] {
    for item in results {
        var title = item["title"] as String
    }
}

注意:我自己没有运行此代码,但我认为您应该明白这一点。