我的 JSON 是给定的格式
{
"BL_NO": "CSV*******13",
"LOADING_PORT": "YANTAI",
"DISCHARGE_PORT": "YINGKOU",
"DELIVERY_PLACE": "YINGKOU"
}
我正在尝试使用下面给出的代码:
NSString *str =[NSString stringWithFormat:@"http://wcfshiptracker.logistify.net/ShipmentTrackingService.svc/FetchData/ChinaShipping-bl_no-TGHU6571642"];
NSURL *url = [NSURL URLWithString:str];
NSData *data = [NSData dataWithContentsOfURL:url];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
aryother = [dict valueForKey:@"BL_NO"];
最佳答案
您的 JSON 格式不正确,它给出的字符串不是字典。请使用这个
NSString *str =[NSString stringWithFormat:@"http://wcfshiptracker.logistify.net/ShipmentTrackingService.svc/FetchData/ChinaShipping-bl_no-TGHU6571642"];
NSURL *url = [NSURL URLWithString:str];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *str3= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSError *jsonError;
NSData *objectData = [str3 dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *dict3 = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingAllowFragments
error:&jsonError];
NSString *a = [dict3 valueForKey:@"BL_NO"];
关于ios - 如何从这种类型的json格式中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38343432/