Questions tagged «nsjsonserialization»

27
使用Swift读取JSON文件
我真的很努力地尝试将JSON文件读入Swift,以便可以使用它。我花了两天的大部分时间来重新搜索并尝试不同的方法,但到目前为止还没有运气,因此我已经注册了StackOverFlow,以查看是否有人可以向我指出正确的方向..... 我的JSON文件称为test.json,其中包含以下内容: { "person":[ { "name": "Bob", "age": "16", "employed": "No" }, { "name": "Vinny", "age": "56", "employed": "Yes" } ] } 该文件直接存储在文档中,我使用以下代码进行访问: let file = "test.json" let dirs : String[] = NSSearchPathForDirectoriesInDomains( NSSearchpathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainMask, true) as String[] if (dirs != nil) { let directories: String[] = dirs let dir …

10
如何将JSON字符串转换为字典?
我想在我的swift项目中做一个将String转换成Dictionary json格式的函数,但是出现一个错误: 无法转换表达式的类型(@lvalue NSData,options:IntegerLitralConvertible ... 这是我的代码: func convertStringToDictionary (text:String) -> Dictionary<String,String> { var data :NSData = text.dataUsingEncoding(NSUTF8StringEncoding)! var json :Dictionary = NSJSONSerialization.JSONObjectWithData(data, options:0, error: nil) return json } 我在Objective-C中实现此功能: - (NSDictionary*)convertStringToDictionary:(NSString*)string { NSError* error; //giving error as it takes dic, array,etc only. not custom object. NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; …

6
无法将类型“ NSTaggedPointerString”的值强制转换为“ NSNumber”
我有一个这样的Swift结构。 struct Usage { var totalData: Double var remainingTotalData: Double init(jsonData: NSData) { var jsonDict = [String: AnyObject]() do { jsonDict = try NSJSONSerialization.JSONObjectWithData(jsonData, options: []) as! [String: AnyObject] } catch { print("Error occurred parsing data: \(error)") } totalData = jsonDict["totalfup"] as! Double remainingTotalData = jsonDict["totalrem"] as! Double } } …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.