开发者

IOS JSON parsing

开发者 https://www.devze.com 2023-02-18 08:11 出处:网络
So i have this JSON: {\"Curs\":[{\"ID\":\"AED\",\"Name\":\"Dirhamul Emiratelor Arabe\",\"Curs\":\"0.8013\",\"Multiplier\":\"1\",\"Data\":\"2011-03-21\"},{\"ID\":\"AUD\",\"Name\":\"Dolarul australian\"

So i have this JSON: {"Curs":[{"ID":"AED","Name":"Dirhamul Emiratelor Arabe","Curs":"0.8013","Multiplier":"1","Data":"2011-03-21"},{"ID":"AUD","Name":"Dolarul australian","Curs":"2.9611","Multiplier":"1","Data":"2011-03-21"},{"ID":"BGN","Name":"Leva bulgareasc","Curs":"2.1314","Multiplier":"1","Data":"2011-03-21"},{"ID":"BRL","Name":"Realul brazilian","Curs":"1.7636","Multiplier":"1","Data":"2011-03-21"},{"ID":"CAD","Name":"Dolarul canadian","Curs":"3.0137","Multiplier":"1","Data":"2011-03-21"},{"ID":"CHF","Name":"Francul elvetian","Curs":"3.2484","Multiplier":"1","Data":"2011-03-21"},{"ID":"CNY","Name":"Renminbi-ul chinezesc","Curs":"0.4483","Multiplier":"1","Data":"2011-03-21"},{"ID":"CZK","Name":"Coroana ceha","Curs":"0.1707","Multiplier":"1","Data":"2011-03-21"},{"ID":"DKK","Name":"Coroana daneza","Curs":"0.559","Multiplier":"1","Data":"2011-03-21"},{"ID":"EGP","Name":"Lira egipteana","Curs":"0.4959","Multiplier":"1","Data":"2011-03-21"},{"ID":"EUR","Name":"Euro","Curs":"4.1685","Multiplier":"1","Data":"2011-03-21"},{"ID":"GBP","Name":"Lira sterlina","Curs":"4.7864","Multiplier":"1","Data":"2011-03-21"},{"ID":"HUF","Name":"100 Forinti maghiari","Curs":"1.5354","Multiplier":"100","Data":"2011-03-21"},{"ID":"INR","Name":"Rupia indiana","Curs":"0.0653","Multiplier":"1","Data":"2011-03-21"},{"ID":"JPY","Name":"100 Yeni japonezi","Curs":"3.6213","Multiplier":"100","Data":"2011-03-21"},{"ID":"KRW","Name":"100 Woni sud-coreeni","Curs":"0.2623","Multiplier":"100","Data":"2011-03-21"},{"ID":"MDL","Name":"Leul moldovenesc","Curs":"0.2507","Multiplier":"1","Data":"2011-03-21"},{"ID":"MXN","Name":"Peso-ul mexican\t","Curs":"0.2452","Multiplier":"1","Data":"2011-03-21"},{"ID":"NOK","Name":"Coroana norvegiana","Curs":"0.5286","Multiplier":"1","Data":"2011-03-21"},{"ID":"NZD","Name":"Dolarul neo-zeel开发者_如何转开发andez","Curs":"2.164","Multiplier":"1","Data":"2011-03-21"},{"ID":"PLN","Name":"Zlotul polonez","Curs":"1.0299","Multiplier":"1","Data":"2011-03-21"},{"ID":"RSD","Name":null,"Curs":"0.0404","Multiplier":"1","Data":"2011-03-21"},{"ID":"RUB","Name":"Rubla ruseasca","Curs":"0.1039","Multiplier":"1","Data":"2011-03-21"},{"ID":"SEK","Name":"Coroana suedeza","Curs":"0.4697","Multiplier":"1","Data":"2011-03-21"},{"ID":"TRY","Name":"Lira turceasca","Curs":"1.8665","Multiplier":"1","Data":"2011-03-21"},{"ID":"UAH","Name":"Hryvna ucraineana","Curs":"0.3699","Multiplier":"1","Data":"2011-03-21"},{"ID":"USD","Name":"Dolarul american","Curs":"2.9428","Multiplier":"1","Data":"2011-03-21"},{"ID":"XAU","Name":"Gramul de aur","Curs":"135.0844","Multiplier":"1","Data":"2011-03-21"},{"ID":"XDR","Name":"DST","Curs":"4.678","Multiplier":"1","Data":"2011-03-21"},{"ID":"ZAR","Name":"Randul sud-african ","Curs":"0.4233","Multiplier":"1","Data":"2011-03-21"}]}

And i want to parse it but i am new to this and don't know how. For example how could i get the values from ID. Here is my code:

#import "DownloadData.h"
#import "JSON.h"


@implementation DownloadData

-(void) connection
{

    // Create new SBJSON parser object
    SBJsonParser *parser = [[SBJsonParser alloc] init];

    // Prepare URL request to download statuses from Twitter
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://source"]];

    // Perform request and get JSON back as a NSData object
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];


    // Get JSON as a NSString from NSData response
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    //NSLog(json_string);
    // parse the JSON response into an object
    // Here we're using NSArray since we're parsing an array of JSON status objects
    NSArray *statuses = [parser objectWithString:json_string error:nil];

    // Each element in statuses is a single status
    // represented as a NSDictionary
    //NSLog([json_string description]);
    //NSLog([json_string objectForKey:@"Curs"]);
    /*
    for (NSDictionary *status in statuses)
    {
        // You can retrieve individual values using objectForKey on the status NSDictionary
        // This will print the tweet and username to the console

        NSLog(@"%@ - %@",[status objectForKey:@"Curs"]);
    }
    */
    NSLog(@"test");
    //jsondownload=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
}
@end


Do:

NSDictionary *responseObj = [parser objectWithString:json_string error:nil];

NSArray *statuses = [responseObj objectForKey:@"Curs"];

for (id anUpdate in statuses) {


  NSLog(@"ID: %@", [(NSDictionary*)anUpdate objectForKey:@"ID"]);

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号