Ankan Majumder Ankan Majumder - 4 months ago 8
iOS Question

How to get data from this type of json format?

My JSON is in given format

{
"BL_NO": "CSV*******13",
"LOADING_PORT": "YANTAI",
"DISCHARGE_PORT": "YINGKOU",
"DELIVERY_PLACE": "YINGKOU"
}


I am trying it using code given below:

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"];

Answer

your JSON is not right format it gives a string not Dictionary. plz use this

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"];
Comments