Translate

Monday, July 9, 2012

Get Values From Json in iphone sdk

First We have add JSON framework on your project code (if u need JSON Code download here : http://www.touch-code-magazine.com/tutorial-fetch-and-parse-json/)  tutorial: json_demo1



#import <UIKit/UIKit.h>

@interface json_demo1ViewController : UIViewController {
IBOutlet UILabel* label;
NSMutableData* responseData;
}

@property (retain, nonatomic) NSMutableData* responseData;

-(IBAction)loadData;

@end



#import "json_demo1ViewController.h"
#import "JSON.h"

#define kLatestKivaLoansURL @"http://api.kivaws.org/v1/loans/search.json?status=fundraising"

@implementation json_demo1ViewController

@synthesize responseData;

#pragma mark -
#pragma mark Fetch loans from internet
-(void)loadData
{
self.responseData = [NSMutableData data];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kLatestKivaLoansURL]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connection release];
self.responseData = nil;
}

#pragma mark -
#pragma mark Process loan data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
NSArray* latestLoans = [(NSDictionary*)[responseString JSONValue] objectForKey:@"loans"];
    NSLog(@"latestLoans %@", latestLoans);
    
[responseString release];
//choose a random loan
NSDictionary* loan = [latestLoans objectAtIndex:0];
//fetch the data
NSNumber* fundedAmount = [loan objectForKey:@"funded_amount"];
NSNumber* loanAmount = [loan objectForKey:@"loan_amount"];
float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];
NSString* name = [loan objectForKey:@"name"];
NSString* country = [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"];
    
    NSLog(@"latestLoans %@ === %@", name, country);
//set the text to the label
label.text = [NSString stringWithFormat:@"Latest loan: %@ from %@ needs another $%.2f, please help",
  name,country,outstandingAmount
  ];
}

@end




Output :

json_demo1[2304:ef03] latestLoans (
        {
        activity = "Clothing Sales";
        "basket_amount" = 0;
        "borrower_count" = 1;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 445627;
        image =         {
            id = 1131782;
            "template_id" = 1;
        };
        "loan_amount" = 925;
        location =         {
            country = "Sierra Leone";
            "country_code" = SL;
            geo =             {
                level = country;
                pairs = "8.5 -11.5";
                type = point;
            };
            town = "Freetown East";
        };
        name = Abu;
        "partner_id" = 148;
        "planned_expiration_date" = "2012-08-09T06:20:04Z";
        "posted_date" = "2012-07-10T06:20:04Z";
        sector = Clothing;
        status = fundraising;
        use = "To buy more clothes so that he can realize more profits.";
    },
        {
        activity = "Grocery Store";
        "basket_amount" = 0;
        "borrower_count" = 1;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 445643;
        image =         {
            id = 1131819;
            "template_id" = 1;
        };
        "loan_amount" = 1050;
        location =         {
            country = "Sierra Leone";
            "country_code" = SL;
            geo =             {
                level = country;
                pairs = "8.5 -11.5";
                type = point;
            };
            town = "Freetown East";
        };
        name = Kadiatu;
        "partner_id" = 148;
        "planned_expiration_date" = "2012-08-09T06:20:04Z";
        "posted_date" = "2012-07-10T06:20:04Z";
        sector = Food;
        status = fundraising;
        use = "To buy more stock to satisfy her customers' desire.";
    },
        {
        activity = "Mobile Phones";
        "basket_amount" = 0;
        "borrower_count" = 1;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 445646;
        image =         {
            id = 1131824;
            "template_id" = 1;
        };
        "loan_amount" = 1175;
        location =         {
            country = "Sierra Leone";
            "country_code" = SL;
            geo =             {
                level = country;
                pairs = "8.5 -11.5";
                type = point;
            };
            town = "Freetown East";
        };
        name = Alex;
        "partner_id" = 148;
        "planned_expiration_date" = "2012-08-09T06:20:04Z";
        "posted_date" = "2012-07-10T06:20:04Z";
        sector = Retail;
        status = fundraising;
        use = "to buy mobile phones to sell";
    },
        {
        activity = "Food Market";
        "basket_amount" = 0;
        "borrower_count" = 1;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 441957;
        image =         {
            id = 1124070;
            "template_id" = 1;
        };
        "loan_amount" = 950;
        location =         {
            country = "Sierra Leone";
            "country_code" = SL;
            geo =             {
                level = country;
                pairs = "8.5 -11.5";
                type = point;
            };
            town = Waterloo;
        };
        name = Hawa;
        "partner_id" = 183;
        "planned_expiration_date" = "2012-08-09T06:20:02Z";
        "posted_date" = "2012-07-10T06:20:03Z";
        sector = Food;
        status = fundraising;
        use = "To buy local foodstuffs such as palm oil, bags of native rice, beans and dried peppers.";
        video =         {
            id = 2608;
            "youtube_id" = sFAfbvzz6TE;
        };
    },
        {
        activity = Manufacturing;
        "basket_amount" = 0;
        "borrower_count" = 1;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 447357;
        image =         {
            id = 897860;
            "template_id" = 1;
        };
        "loan_amount" = 725;
        location =         {
            country = "Viet Nam";
            "country_code" = VN;
            geo =             {
                level = country;
                pairs = "16.166667 107.833333";
                type = point;
            };
            town = "Ho Chi Minh City";
        };
        name = An;
        "partner_id" = 172;
        "planned_expiration_date" = "2012-08-09T05:50:04Z";
        "posted_date" = "2012-07-10T05:50:04Z";
        sector = Manufacturing;
        status = fundraising;
        use = "To buy materials to increase her production of candles.";
    },
        {
        activity = "Personal Housing Expenses";
        "basket_amount" = 0;
        "borrower_count" = 1;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 447326;
        image =         {
            id = 1134660;
            "template_id" = 1;
        };
        "loan_amount" = 775;
        location =         {
            country = Mongolia;
            "country_code" = MN;
            geo =             {
                level = country;
                pairs = "46 105";
                type = point;
            };
            town = "Huchit shonhor zah, Ulaanbaatar";
        };
        name = Uudus;
        "partner_id" = 116;
        "planned_expiration_date" = "2012-08-09T05:50:03Z";
        "posted_date" = "2012-07-10T05:50:03Z";
        sector = Housing;
        status = fundraising;
        use = "To build her new house with headers and vacuum windows";
    },
        {
        activity = Agriculture;
        "basket_amount" = 0;
        "borrower_count" = 2;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 447325;
        image =         {
            id = 1131760;
            "template_id" = 1;
        };
        "loan_amount" = 475;
        location =         {
            country = Cambodia;
            "country_code" = KH;
            geo =             {
                level = country;
                pairs = "13 105";
                type = point;
            };
            town = "Battambang province, Sangke district";
        };
        name = "Kai's Group";
        "partner_id" = 204;
        "planned_expiration_date" = "2012-08-09T05:30:03Z";
        "posted_date" = "2012-07-10T05:30:03Z";
        sector = Agriculture;
        status = fundraising;
        use = "To buy fertilizers for rice farming and grocery items to sell";
    },
        {
        activity = "General Store";
        "basket_amount" = 0;
        "borrower_count" = 1;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 447356;
        image =         {
            id = 897855;
            "template_id" = 1;
        };
        "loan_amount" = 500;
        location =         {
            country = "Viet Nam";
            "country_code" = VN;
            geo =             {
                level = country;
                pairs = "16.166667 107.833333";
                type = point;
            };
            town = "Ho Chi Minh City";
        };
        name = Khenh;
        "partner_id" = 172;
        "planned_expiration_date" = "2012-08-09T05:30:03Z";
        "posted_date" = "2012-07-10T05:30:03Z";
        sector = Retail;
        status = fundraising;
        use = "to stock her general store with a greater range and quantity of products.";
    },
        {
        activity = Agriculture;
        "basket_amount" = 0;
        "borrower_count" = 2;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 447323;
        image =         {
            id = 1133987;
            "template_id" = 1;
        };
        "loan_amount" = 500;
        location =         {
            country = Cambodia;
            "country_code" = KH;
            geo =             {
                level = country;
                pairs = "13 105";
                type = point;
            };
            town = "Banteay Meanchey, Mongkul Borey District";
        };
        name = "Sinat's Group";
        "partner_id" = 204;
        "planned_expiration_date" = "2012-08-09T05:30:02Z";
        "posted_date" = "2012-07-10T05:30:02Z";
        sector = Agriculture;
        status = fundraising;
        use = "to buy fertilizers and more sculptured items";
    },
        {
        activity = Farming;
        "basket_amount" = 0;
        "borrower_count" = 2;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 447324;
        image =         {
            id = 1133979;
            "template_id" = 1;
        };
        "loan_amount" = 250;
        location =         {
            country = Cambodia;
            "country_code" = KH;
            geo =             {
                level = country;
                pairs = "13 105";
                type = point;
            };
            town = "Banteay Meanchey, Mongkul Borey District";
        };
        name = "Hing's Group";
        "partner_id" = 204;
        "planned_expiration_date" = "2012-08-09T05:10:03Z";
        "posted_date" = "2012-07-10T05:10:03Z";
        sector = Agriculture;
        status = fundraising;
        use = "To buy fertilizers for her farmland.";
    },
        {
        activity = Retail;
        "basket_amount" = 0;
        "borrower_count" = 1;
        description =         {
            languages =             (
                fr,
                en
            );
        };
        "funded_amount" = 0;
        id = 448167;
        image =         {
            id = 1136502;
            "template_id" = 1;
        };
        "loan_amount" = 750;
        location =         {
            country = Benin;
            "country_code" = BJ;
            geo =             {
                level = town;
                pairs = "6.483333 2.616667";
                type = point;
            };
            town = "Porto-Novo";
        };
        name = "Noussi Eug\U00e9nie";
        "partner_id" = 179;
        "planned_expiration_date" = "2012-08-09T04:40:02Z";
        "posted_date" = "2012-07-10T04:40:02Z";
        sector = Retail;
        status = fundraising;
        use = "to buy 2 packets of hair extensions, 3 packets of weaves, 2 containers of gel, and a 100-gram container of hair product";
    },
        {
        activity = "Food Production/Sales";
        "basket_amount" = 25;
        "borrower_count" = 1;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 447320;
        image =         {
            id = 1134642;
            "template_id" = 1;
        };
        "loan_amount" = 100;
        location =         {
            country = Philippines;
            "country_code" = PH;
            geo =             {
                level = country;
                pairs = "13 122";
                type = point;
            };
        };
        name = Tessie;
        "partner_id" = 128;
        "planned_expiration_date" = "2012-08-09T04:30:02Z";
        "posted_date" = "2012-07-10T04:30:02Z";
        sector = Food;
        status = fundraising;
        use = "to buy raw ingredients for making snacks.";
    },
        {
        activity = Farming;
        "basket_amount" = 0;
        "borrower_count" = 2;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 447313;
        image =         {
            id = 1131725;
            "template_id" = 1;
        };
        "loan_amount" = 425;
        location =         {
            country = Cambodia;
            "country_code" = KH;
            geo =             {
                level = country;
                pairs = "13 105";
                type = point;
            };
            town = "Battambang province, Ek Phnom district";
        };
        name = "Sokha's Group";
        "partner_id" = 204;
        "planned_expiration_date" = "2012-08-09T04:20:18Z";
        "posted_date" = "2012-07-10T04:20:18Z";
        sector = Agriculture;
        status = fundraising;
        use = "buy fertilizer and a water pump";
    },
        {
        activity = "Food Production/Sales";
        "basket_amount" = 0;
        "borrower_count" = 1;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 25;
        id = 447314;
        image =         {
            id = 1134637;
            "template_id" = 1;
        };
        "loan_amount" = 175;
        location =         {
            country = Philippines;
            "country_code" = PH;
            geo =             {
                level = town;
                pairs = "10.149167 124.325";
                type = point;
            };
            town = "Talibon, Bohol";
        };
        name = Bienvenida;
        "partner_id" = 125;
        "planned_expiration_date" = "2012-08-09T04:20:18Z";
        "posted_date" = "2012-07-10T04:20:18Z";
        sector = Food;
        status = fundraising;
        use = "buy flour, sugar and cooking oil ";
    },
        {
        activity = Farming;
        "basket_amount" = 0;
        "borrower_count" = 2;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 447315;
        image =         {
            id = 1131748;
            "template_id" = 1;
        };
        "loan_amount" = 500;
        location =         {
            country = Cambodia;
            "country_code" = KH;
            geo =             {
                level = country;
                pairs = "13 105";
                type = point;
            };
            town = "Battambang province, Ek Phnom district";
        };
        name = "Ra's Group";
        "partner_id" = 204;
        "planned_expiration_date" = "2012-08-09T04:20:18Z";
        "posted_date" = "2012-07-10T04:20:18Z";
        sector = Agriculture;
        status = fundraising;
        use = "buy fertilizer and a pipe";
    },
        {
        activity = Fishing;
        "basket_amount" = 0;
        "borrower_count" = 2;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 50;
        id = 447316;
        image =         {
            id = 1131730;
            "template_id" = 1;
        };
        "loan_amount" = 500;
        location =         {
            country = Cambodia;
            "country_code" = KH;
            geo =             {
                level = country;
                pairs = "13 105";
                type = point;
            };
            town = "Battambang province, Ek Phnom district";
        };
        name = "Sinang's Group";
        "partner_id" = 204;
        "planned_expiration_date" = "2012-08-09T04:20:18Z";
        "posted_date" = "2012-07-10T04:20:18Z";
        sector = Food;
        status = fundraising;
        use = "buy a fishing boat";
    },
        {
        activity = "Fish Selling";
        "basket_amount" = 0;
        "borrower_count" = 1;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 447317;
        image =         {
            id = 1134639;
            "template_id" = 1;
        };
        "loan_amount" = 125;
        location =         {
            country = Philippines;
            "country_code" = PH;
            geo =             {
                level = country;
                pairs = "13 122";
                type = point;
            };
        };
        name = Susan;
        "partner_id" = 128;
        "planned_expiration_date" = "2012-08-09T04:20:18Z";
        "posted_date" = "2012-07-10T04:20:18Z";
        sector = Food;
        status = fundraising;
        use = "to buy a variety of fish to resell.";
    },
        {
        activity = Farming;
        "basket_amount" = 0;
        "borrower_count" = 2;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 0;
        id = 447318;
        image =         {
            id = 1133983;
            "template_id" = 1;
        };
        "loan_amount" = 600;
        location =         {
            country = Cambodia;
            "country_code" = KH;
            geo =             {
                level = country;
                pairs = "13 105";
                type = point;
            };
            town = "Banteay Meanchey, Mongkul Borey District";
        };
        name = "Thorn's Group";
        "partner_id" = 204;
        "planned_expiration_date" = "2012-08-09T04:20:18Z";
        "posted_date" = "2012-07-10T04:20:18Z";
        sector = Agriculture;
        status = fundraising;
        use = "To buy fertilizers and grocery items.";
    },
        {
        activity = Farming;
        "borrower_count" = 1;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 200;
        id = 447319;
        image =         {
            id = 1134640;
            "template_id" = 1;
        };
        "loan_amount" = 200;
        location =         {
            country = Philippines;
            "country_code" = PH;
            geo =             {
                level = country;
                pairs = "13 122";
                type = point;
            };
            town = "San Jose, Talibon, Bohol";
        };
        name = Rosalina;
        "partner_id" = 125;
        "planned_expiration_date" = "2012-08-09T04:20:18Z";
        "posted_date" = "2012-07-10T04:20:18Z";
        sector = Agriculture;
        status = funded;
        use = "to buy rice seedlings and fertilizer and pay for maintenance for the equipment (a tractor and other items).";
    },
        {
        activity = Pigs;
        "basket_amount" = 0;
        "borrower_count" = 3;
        description =         {
            languages =             (
                en
            );
        };
        "funded_amount" = 50;
        id = 447322;
        image =         {
            id = 1133985;
            "template_id" = 1;
        };
        "loan_amount" = 625;
        location =         {
            country = Cambodia;
            "country_code" = KH;
            geo =             {
                level = country;
                pairs = "13 105";
                type = point;
            };
            town = "Banteay Meanchey, Mongkul Borey District";
        };
        name = "Phoeurt's Group";
        "partner_id" = 204;
        "planned_expiration_date" = "2012-08-09T04:20:18Z";
        "posted_date" = "2012-07-10T04:20:18Z";
        sector = Agriculture;
        status = fundraising;
        use = "To buy pigs and pig feed";
    }
)
2012-07-10 11:55:47.513 json_demo1[2304:ef03] latestLoans Abu === Sierra Leone

No comments:

Post a Comment