json交互时,要考虑不同语言下对几种常用类型的处理,比如oc中对boolean布尔型的处理,直接用boolValue去处理true/false是没有问题的,但换成True或者TRUE,就有问题了,所以,需要考虑清楚用哪种类型来交互。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | NSString *jsonString=@ "{\"result\":true,\"status\":\"success\",\"number\":0}" ; NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError *err; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err]; NSString *str1=@ "success" ; if ([str1 isEqualToString:[dic valueForKey:@ "status" ]]){ NSLog(@ "相同" ); } else { NSLog(@ "不同" ); } if ([[dic valueForKey:@ "result" ]boolValue]){ NSLog(@ "真" ); } else { NSLog(@ "假" ); } if ([[dic valueForKey:@ "number" ]boolValue]){ NSLog(@ "真" ); } else { NSLog(@ "假" ); } |
联系客服