问题描述
我正在使用一个Swift应用程序来读取JSON数据
如果JSON数据不好或不存在,则没有问题。 JSON对象从不实例化。
但是,如果JSON数据是好的JSON,但不是我想要的,对象实例化,但包含一个结构,我找到一个运行时错误。
我看了使用Swift的RTTI(dynamicType),但总是返回< Swift.AnyObject>
我想要的JSON是一个特定的格式:字典数组:
[[String:String]]! JSON:[{key:value},{key,value},{Key:value}]
如果我喂它一个单一的元素:
{Key: value}
例程我试图强制转换,但失败。
我想测试JSON对象,以确保它具有我想要的结构,在转换之前。
if(nil!= inData){
let rawJSONObject:AnyObject? = NSJSONSerialization.JSONObjectWithData(inData,options:nil,error:nil)
println(Type:\(rawJSONObject.dynamicType))
if(nil!= rawJSONObject){
/ /下面的行下面的BLOWS UP如果我喂它BAD / GOODJSON:
let jsonObject:[[String:String]]! = rawJSONObject as! [[String:String]]!
//我需要在做CAST之前测试
if((nil!= jsonObject)&&&(0< jsonObject.count)){
let jsonDictionary:[String :String] = jsonObject [0]
if(1== jsonDictionary [semanticAdmin]){//我们必须设置语义管理标志。
let testString:String! = jsonDictionary [versionInt]
if(nil!= testString){
let version = testString.toInt()
if(version> .s_minServerVersion){//必须是一个有效的版本,让我们注意。
self.serverVersionAsInt = version!
}
}
}
}
}
}
我的问题是,有没有一个好的方法来测试一个NSJSONSerialization响应结构的JSON在uwinding /转换之前?
我觉得可能更接近我需要的,但我有
您可以使用安全展开来测试原始对象的类型,示例:
如果let jsonObject = rawJSONObject as? [[String:String]] {
// jsonObject是一个字典数组
} else if let jsonObject = rawJSONObject as? [String:String] {
// jsonObject是一个字典
//你可以根据自己的需要来匹配它,例如把它放在一个数组中
} else {
//失败,rawJSONObject是另一种类型
}
目前,您的代码崩溃,解开,!
,如果转换失败,值为nil。
I suspect that I am not quite grokking Swift 1.2, and I need to RTFM a bit more.
I'm working on a Swift app that reads JSON data from a URI.
If the JSON data is bad, or nonexistent, no issue. The JSON object never instantiates.
However, if the JSON data is good JSON, but not what I want, the object instantiates, but contains a structure that is not what I'm looking for, I get a runtime error.
I looked at using Swift's "RTTI" (dynamicType), but that always returns "<Swift.AnyObject>", no matter what the data is.
I want the JSON to be a specific format: An array of Dictionaries:
[[String:String]]! JSON: [{"key":"value"},{"key","value"},{"Key":"value"}]
If I feed it a single element:
{"Key":"value"}
The routine I have tries to cast it, and that fails.
I want to test the JSON object to make sure that it has a structure I want before casting.
if(nil != inData) {
let rawJSONObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(inData, options: nil, error: nil)
println("Type:\(rawJSONObject.dynamicType)")
if(nil != rawJSONObject) {
// THE LINE BELOW BLOWS UP IF I FEED IT "BAD/GOOD" JSON:
let jsonObject: [[String:String]]! = rawJSONObject as! [[String:String]]!
// I NEED TO TEST IT BEFORE DOING THE CAST
if((nil != jsonObject) && (0 < jsonObject.count)) {
let jsonDictionary: [String:String] = jsonObject[0]
if("1" == jsonDictionary["semanticAdmin"]) { // We have to have the semantic admin flag set.
let testString: String! = jsonDictionary["versionInt"]
if(nil != testString) {
let version = testString.toInt()
if(version >= self.s_minServerVersion) { // Has to be a valid version for us to pay attention.
self.serverVersionAsInt = version!
}
}
}
}
}
}
My question is, is there a good way to test an NSJSONSerialization response for the structure of the JSON before uwinding/casting it?
I feel as if this question may be closer to what I need, but I am having trouble "casting" it to my current issue.
You can use safe unwrapping to test the type of your raw object, for example:
if let jsonObject = rawJSONObject as? [[String:String]] {
// jsonObject is an array of dictionaries
} else if let jsonObject = rawJSONObject as? [String:String] {
// jsonObject is a dictionary
// you can conform it as you wish, for example put it in an array
} else {
// fail, rawJSONObject is of another type
}
Currently, your code crashes because of the forced unwrapping, with !
, of values that will be nil if the cast fails.
这篇关于安全动态JSON在Swift中转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!