ios - 如何从这种类型的json格式中获取数据?

标签 ios objective-c

我的 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/

相关文章:

ios - Capturer(iPhone 摄像头)未提供给 TVIVideoCapturer for TwilioVideo iOS SDK

ios - 检测 mapItemForCurrentLocation 何时无法找到您的设备

ios - 创建类似 Instagram 的照片提要

ios - 如何获取在 CoreData 中获取平均值的实体属性

ios - 如何检查服务器是否可访问并正在运行?

ios - AWS Cognito 在 Swift 中获取用户组

ios - 如何在 UITextView 中设置光标的位置

ios - Xcode 4.3.1 语法错误

ios - 仪器之间的区别(僵尸和泄漏)

objective-c - 使有关释放 CGMutablePathRef 对象的编译器警告静音