为什么两个值中的一个(它们似乎是一个字符串)被转换为双精度值,而第二个不是在从 JSON 解析到 Swift 时?
Why one of the two values (they seem to be a Strings) is converted to Double, and the second one is not while parsing from JSON in Swift?
这是我的示例词典:
{
"gross_price" = "6.5678565676";
"gross_price_total" = "6.00";
}
现在我解析该值:
if let grossPriceTotal = dictionary["gross_price_total"] as? Double {
order!.grossPriceTotal = grossPriceTotal
//doesn't work
}
if let grossPriceTotal = dictionary["gross_price"] as? Double {
order!.grossPriceTotal = grossPriceTotal
//works
}
为什么第一个条件没有转换,第二个是?
这是应用程序的真实屏幕:
我想知道为什么在这种情况下它确实很容易转换为 Double
,因为它看起来像 String
,不应该转换?
简答:
dictionary["gross_price_total"]
是一个 字符串 ,因此
as? Double
失败。
dictionary["gross_price"]
是一个 数字 ,因此 as? Double
成功。 然而、println(dictionary)
将此数字打印为
"6.5678565676"
,因此它看起来 像一个字符串。
长答案:
下面是演示问题的完整示例:
let jsonString = "{ \"gross_price\" : 5.23, \"gross_price_total\" : \"6.00\" }"
println("JSON: \(jsonString)")
let jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!
var error : NSError?
if let dictionary : AnyObject = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) {
println("dictionary: \(dictionary)")
if let grossPriceTotal = dictionary["gross_price_total"] as? Double {
println(grossPriceTotal)
} else {
println("no grossPriceTotal")
}
if let grossPriceTotal = dictionary["gross_price"] as? Double {
println(grossPriceTotal)
} else {
println("no gross_price")
}
} else {
println(error)
}
输出:
JSON: { "gross_price" : 5.23, "gross_price_total" : "6.00" }
dictionary: {
"gross_price" = "5.23";
"gross_price_total" = "6.00";
}
no grossPriceTotal
5.23
"gross_price"的值是一个数字,但是打印字典
将其显示为 字符串 。这个数字可以用as? Double
.
转换
"gross_price_total"的值是一个字符串,不能
用 as? Double
.
转换
所以混淆仅来自于 println(dictionary)
将带小数位的数字用引号括起来,
这样它们就无法与字符串区分开来。
NSArray
和 NDDictionary
的描述格式在 Old-Style ASCII Property Lists 中有描述(强调已添加):
A string is enclosed in double quotation marks, for example:
"This is a string"
The
quotation marks can be omitted if the string is composed strictly of
alphanumeric characters and contains no white space (numbers are
handled as strings in property lists).
这是我的示例词典:
{
"gross_price" = "6.5678565676";
"gross_price_total" = "6.00";
}
现在我解析该值:
if let grossPriceTotal = dictionary["gross_price_total"] as? Double {
order!.grossPriceTotal = grossPriceTotal
//doesn't work
}
if let grossPriceTotal = dictionary["gross_price"] as? Double {
order!.grossPriceTotal = grossPriceTotal
//works
}
为什么第一个条件没有转换,第二个是?
这是应用程序的真实屏幕:
我想知道为什么在这种情况下它确实很容易转换为 Double
,因为它看起来像 String
,不应该转换?
简答:
dictionary["gross_price_total"]
是一个 字符串 ,因此as? Double
失败。dictionary["gross_price"]
是一个 数字 ,因此as? Double
成功。 然而、println(dictionary)
将此数字打印为"6.5678565676"
,因此它看起来 像一个字符串。
长答案:
下面是演示问题的完整示例:
let jsonString = "{ \"gross_price\" : 5.23, \"gross_price_total\" : \"6.00\" }"
println("JSON: \(jsonString)")
let jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!
var error : NSError?
if let dictionary : AnyObject = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) {
println("dictionary: \(dictionary)")
if let grossPriceTotal = dictionary["gross_price_total"] as? Double {
println(grossPriceTotal)
} else {
println("no grossPriceTotal")
}
if let grossPriceTotal = dictionary["gross_price"] as? Double {
println(grossPriceTotal)
} else {
println("no gross_price")
}
} else {
println(error)
}
输出:
JSON: { "gross_price" : 5.23, "gross_price_total" : "6.00" } dictionary: { "gross_price" = "5.23"; "gross_price_total" = "6.00"; } no grossPriceTotal 5.23
"gross_price"的值是一个数字,但是打印字典
将其显示为 字符串 。这个数字可以用as? Double
.
"gross_price_total"的值是一个字符串,不能
用 as? Double
.
所以混淆仅来自于 println(dictionary)
将带小数位的数字用引号括起来,
这样它们就无法与字符串区分开来。
NSArray
和 NDDictionary
的描述格式在 Old-Style ASCII Property Lists 中有描述(强调已添加):
A string is enclosed in double quotation marks, for example:
"This is a string"
The quotation marks can be omitted if the string is composed strictly of alphanumeric characters and contains no white space (numbers are handled as strings in property lists).