解析 JSON 响应中的整数时出现问题
我有以下 JSON 响应字符串,
{"firstname":"a","lastname":"a","jobtitle":"software developer","companyname":"abc","mobileno":9461438988}
但无法获取“mobileno”字段的字符串值 这就是我读取“mobileno”字段的方式,
self.dispPhone = [NSString stringWithFormat:@"%@ ",[parsedProfileData valueForKey:@"mobileno"]];
当我 NSLog 时,这里的字符串变成一些垃圾值“2147483647”
I have following JSON response string
{"firstname":"a","lastname":"a","jobtitle":"software developer","companyname":"abc","mobileno":9461438988}
i cant get the string value for 'mobileno' field
This is how i read the 'mobileno' field
self.dispPhone = [NSString stringWithFormat:@"%@ ",[parsedProfileData valueForKey:@"mobileno"]];
the string here becomes some garbage value '2147483647' when i NSLog it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,电话号码不是整数。电话号码可能以有效的 0 或
+
开头。例如,在德国,00..
表示国际,0
表示国内,以及任何其他数字区域。返回值是可能的最高 31 位数字。由于您的 JSON 编码器解码为带符号的 32 位整数,因此这是它可以为您提供的最佳值。
要解决此问题,请将电话号码格式化为原始字符串。
First of all, a phone number is not an integer. Phone numbers may start with significant 0s or
+
. For example, in Germany00..
is international,0
national, and any other digit regional.The returned value is the highest possible 31 bit number. Since your JSON encoder decodes to signed 32bit ints, this is the best value it can give you.
To solve this problem, format phone numbers as strings in the original.
数字
9,461,438,988
无法存储在 32 位值中,因此它会被截断以适合。您应该将电话号码存储为字符串(就像邮政编码一样)。
一般来说,任何您不想进行算术或数字比较的内容(
<
或>
)都是字符串,而不是数字。The number
9,461,438,988
cannot be stored in a 32-bit value, so it's being truncated to fit.You should store your phone numbers as the strings they are (just like ZIP codes).
In general, anything that you don't want to do arithmetic or numeric comparison on (
<
or>
) is a string, not a number.