2016-07-13 61 views
1

我的JSON是在給定的格式如何從這種類型的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格式不對,它給出的字符串不是字典。 plz使用這個

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

使一類實體BL_NO,LODING_PORT,DISCHARGE_PORT,DELIVERY_PLACE。 和include Newtonsoft.Json dll使用JsonConvert.DeserializeObject`「<」YourClassName「>」(yourString)。

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). 
+1

什麼是Newtonsoft.Json dll?請給我一個示例代碼。 –

+0

它是一個DLL,你可以通過使用nuget包管理器來獲取它。 – Dev

+0

@Dev你確實意識到OP問及Objective-C問題了嗎? – Lukas

相關問題