简体   繁体   中英

How to parse nsdictionary data using nsserlization.?

I have dictionary data like this and one array on image.

{ "result":"Successful","data":{"id":"12","product_name":"12\\" Round Plate","sku":"ECOW12RP","description":"Diameter 12 inch x\\tDepth 0.9 inch","price":"153.00","business_price":"365.00","image":[{"image":"1454499068ecow12rp_01.jpg"}],"pack_size":"20","business_pack_size":"50","category":"2,3","tax_class":"1","created":"2016-02-03","altered":"2016-02-03 17:52:58","status":"1","deleted":"0","arrange":"1","delivery":"150.00"}}

I want to parse all the key values from it. this is the code which i use for this task.

    -(void)viewDidLoad
{
    NSLog(@"d %ld", (long)id);
    NSString* myNewString = [NSString stringWithFormat:@"%i", id];
    NSURL *producturl = [NSURL  URLWithString:@"http://dev1.brainpulse.org/ecoware1/webservices/product/"   ];
    NSURL *url = [NSURL URLWithString:myNewString  relativeToURL:producturl];
    NSData * imageData = [NSData dataWithContentsOfURL:url];
    UIImage * productimage = [UIImage imageWithData:imageData];
    NSURL *absURL = [url absoluteURL];
    NSLog(@"absURL = %@", absURL);

    NSURLRequest *request= [NSURLRequest requestWithURL:absURL];
    [NSURLConnection connectionWithRequest:request delegate:self];
}

-(void)connection:(NSURLConnection *)connection   didReceiveResponse:(nonnull NSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
    NSLog(@"Did receive response");
}
-(void)connection:(NSURLConnection *)connection  didReceiveData:(NSData *)thedata
{
    [data appendData:thedata];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"data"];

    NSLog(@"arr %@", dictionary);
    [productdetail removeAllObjects];

    for (NSString *tmp in dictionary)

        NSMutableDictionary *temp = [NSMutableDictionary new];
    [temp setObject:@"product_name" forKey:@"product_name"];

    //[temp setObject:[tmp objectForKey:@"id"] forKey:@"id"];
    // [temp setObject:[tmp objectForKey:@"image"] forKey:@"image"];

    [productdetail addObject:temp];

    NSLog(@"detail %@", productdetail);
}

I tried to parse string from nsdictionary with the help of for loop, but I get product details array null, i don't know why it not get key value.

i am parse data which is in nsdictionary but i have null array when i try to parse image array in this json data please look at this json data.

{"result":"Successful","data":{"id":"2","product_name":"6\" Round Plate","sku":"ECOW6RP","description":"Diameter 6.0 (inch) x Depth 0.6 (inch)\r\n\r\nPerfect for finger foods!","price":"42.89","business_price":"100.00","image":[{"image":"1454499251ecow6rp_01.jpg"}],"pack_size":"20","business_pack_size":"50","category":"2,3","tax_class":"1","created":"2016-01-19","altered":"2016-02-06 16:06:10","status":"1","deleted":"0","arrange":"1","delivery":"150.00"}}

Regarding your specific question to get the product_name data into your dictionary, this will work

   NSDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"data"];

   NSMutableDictionary *temp = [NSMutableDictionary new];

if ([dictionary objectForKey:@"product_name"]){
   [temp setObject:[dictionary objectForKey:@"product_name"] forKey:@"product_name"];
}

If you print out the dictionary you made, you should see it is in there.

NSLog(@"the temp dictionary value for ProductName: %@", [temp objectForKey:@"product_name"];

try this

Option-1

NSDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"data"];

[productdetail removeAllObjects];

if (dictionary)
{

NSMutableDictionary *temp = [NSMutableDictionary new];
[temp setObject:[dictionary objectForKey:@"product_name"] forKey:@"product_name"];
 [productdetail addObject:temp];
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM