Ios 如何从这种json格式获取数据?
我的JSON是给定格式的Ios 如何从这种json格式获取数据?,ios,objective-c,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/ShipmentTrackingSe
{
"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"];
用实体
提单号、寄存港、卸货港、交货地点创建一个类
。
和包含Newtonsoft.Json dll
使用JsonConvert.DeserializeObject`(您的字符串)
公共类您的类
{
公共字符串BL_NO{get;set;}
公共字符串存放_端口{get;set;}
公共字符串排出口{get;set;}
公共字符串传递位置{get;set;}
}
字符串yourJsonString=“{\'BL\'U NO\”:\“CSV**********13\”,“装货港”:“烟台”,“卸货港”:“营口”,“交货地点”:“营口”
YourClass obj=JsonConvert.DeserializeObject“”(yourJsonString)。
您的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"];
什么是Newtonsoft.Json dll?请给我一个示例代码。@Dev您知道OP问了一个Objective-C问题,对吗?
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"];