注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

熊猫正正的博客

熊猫正正的天空

 
 
 

日志

 
 

在IOS5中使用JSON编程  

2013-04-13 23:51:49|  分类: ios开发 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
在IOS5中使用JSON编程,ViewController.h代码如下:

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController


@property (strong, nonatomic) IBOutlet UILabel *humanReadable;

@property (strong, nonatomic) IBOutlet UILabel *jsonSummary;


@end


ViewController.m代码如下:

//1 代码

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

//2代码

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


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController

@synthesize humanReadable;

@synthesize jsonSummary;


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    dispatch_async(kBgQueue, ^{

        NSData *data = [NSData dataWithContentsOfURL:kLatestKivaLoansURL];

        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];

    });

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (void) fetchedData: (NSData *) responseData

{

    NSError *error;

    NSDictionary *json = [NSJSONSerialization

                          JSONObjectWithData:responseData options:kNilOptions

                          error:&error];

    NSArray *latestLoans = [json objectForKey:@"loans"];

    NSLog(@"loans: %@",latestLoans);

    

    //get the latest loan

    NSDictionary *loan = [latestLoans objectAtIndex:0];

    

    //get the funded amount and loan amount

    NSNumber *fundedAmount = [loan objectForKey:@"funded_amount"];

    NSNumber *loanAmount = [loan objectForKey:@"loan_amount"];

    float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];

    

    //set the label appropriately

    humanReadable.text = [NSString stringWithFormat:@"最新货款: %@  来自  %@  需要另外的 %.2f US$追求创业梦!",

                          [loan objectForKey:@"name"],

                          [(NSDictionary *)[loan objectForKey:@"location"]

                           objectForKey:@"country"],

                          outstandingAmount];

    

    NSLog(@"welcome to pandazheng blog");

    

    //build an info object and convert to json

    NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:

                          [loan objectForKey:@"name"],

                          @"who",

                          [(NSDictionary *)[loan objectForKey:@"location"]objectForKey:@"country"],

                          @"where",

                          [NSNumber numberWithFloat:outstandingAmount],

                          @"what",

                          nil];

    

    //convert object to data

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:&error];

    

    //print out the data contents

    jsonSummary.text = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

}


@end



  评论这张
 
阅读(58)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017