简体   繁体   中英

How to parse the data of JSON response in Objective-C?

I got a JSON output in this link

I extracted the id in this output. (ie)

419023188150340,
419023228150336,
419023278150331,
419023318150327,
419044998148159,
419045028148156

using the following code

[[dict objectForKey:@"data"]valueForKey:@"id"];

dict has the total output that is displayed on the link

I displayed this in tableview.
Now if once i tap the cell in tableview. The table should fetch the value of "picture" and display it in console( NSLog ).

I used the following code to get the response and store it in dictionary(here dict)

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
    responseData=nil;
    idArray=nil;
    responseData=[[NSMutableData alloc]init];
    idArray=[[NSMutableArray alloc]init];
    dict=[[NSDictionary alloc]init];   
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    [responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    dict=[NSJSONSerialization JSONObjectWithData:responseData options:0 error:NULL];
    [idArray addObjectsFromArray:[[dict objectForKey:@"data"]valueForKey:@"id"]];
    [tableVw reloadData];
}

Can any one tell me how to do this.

You are doing it wrong. You first get a NSArray of IDS and you have to loop over it to extract each ID. Try this -

NSArray *fbIds = [dict objectForKey:@"data"];
for(NSDictionary *fbId in fbIds)
{
    NSLog("FB ID: %@", [fbID valueForKey:@"id"]);
}

Once you extract the correct Facebook_ID, you can use http://graph.facebook.com/419023188150340/picture to get your picture. Hope this helps...

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