Jam*_*mes 22 json objective-c ios
我有这个奇怪的问题.NSDictionary未返回正确的整数值.
来自服务器的JSON响应代码.
{
"status":"ok",
"error_code":0,
"data" : [],
"msg":"everything is working!"
}
Run Code Online (Sandbox Code Playgroud)
JSON正在转换为NSDictionary.
NSError *error = nil;
NSDictionary *jsonDict = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error: &error];
Run Code Online (Sandbox Code Playgroud)
我使用以下代码访问NSDictionary值.
int error_code = (int)[jsonDict valueForKey:@"error_code"]
NSLog(@"%i", error_code);
The log outputs the following: 143005344
Run Code Online (Sandbox Code Playgroud)
我甚至尝试过objectForKey并得到相同的响应.
提前致谢.
Lef*_*ris 54
是的它输出值的指针,这就是你看到这个日志输出的原因.
您不能将指针强制转换为整数并期望该值.
int error_code = [[jsonDict valueForKey:@"error_code"] integerValue];
Run Code Online (Sandbox Code Playgroud)
或者如果你想使用现代的目标-c
int error_code = [jsonDict[@"error_code"] integerValue];
Run Code Online (Sandbox Code Playgroud)
数字作为NSNumber
实例存储在plist/json词典中.试着NSNumber
像intValue
以下人士一样打电话:
int error_code = [jsonDict[@"error_code"] intValue];
Run Code Online (Sandbox Code Playgroud)
另请注意字典的新下标语法.
归档时间: |
|
查看次数: |
16884 次 |
最近记录: |