我的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"];
答案 0 :(得分:0)
你的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"];
答案 1 :(得分:-1)
使用实体BL_NO,LODING_PORT,DISCHARGE_PORT,DELIVERY_PLACE
创建一个类。
include Newtonsoft.Json dll
使用JsonConvert.DeserializeObject`"<" YourClassName">"(#String)。
Public class YourClass
{
public string BL_NO{get;set;}
public string LODING_PORT{get;set;}
public string DISCHARGE_PORT{get;set;}
public string DELIVERY_PLACE{get;set;}
}
string yourJsonString="{\"BL_NO\":\"CSV*******13\",\"LOADING_PORT\":\"YANTAI\",\"DISCHARGE_PORT\":\"YINGKOU\",\"DELIVERY_PLACE\":\"YINGKOU\"}"
YourClass obj=JsonConvert.DeserializeObject "<"YourClass">"(yourJsonString).