Corona中文站

强大、易学的跨平台(iOS/Android)开发框架。QQ群1:74390406(满) 群2:221929599

导航

在Xcode中安装部署及使用JSON.framework(附代码示例)
在Xcode中安装部署JSON
1.下载JSON_2.3.dmg到电脑,双击安装.下载地址:http://code.google.com/p/json-framework/downloads/list?can=1&;q=&colspec=Filename+Summary+Uploaded+ReleaseDate+Size+DownloadCount
如下图:


2.把上图中JSON目录拖拽到Xcode中classes目录,然后在自己的单元文件中就可以使用JSON了(例如:#import "JSON/JSON.h")。

#import "JSONTest1ViewController.h"

#import "JSON/JSON.h"

@implementation JSONTest1ViewController


- (void)viewDidLoad {

[super
viewDidLoad];


/* --- json data ----

[{"id":1,"name":"jack"},{"id":2,"name":"lucy"}]


*/


responseData = [[NSMutableData
data] retain];


NSURLRequest *request = [NSURLRequest
requestWithURL:[NSURL
URLWithString:@"http://192.168.0.160/json.txt"]];


[[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 {


label.text = [NSString
stringWithFormat:@"Connection failed: %@", [error description]];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {


[connection release];


NSString *responseString = [[NSString
alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];


[responseData
release];


NSArray *luckyNumbers = [responseString JSONValue];


NSMutableString *text = [NSMutableString
stringWithString:@"json data:\n"];


for (int i = 0; i < [luckyNumbers count]; i++)


{


[text appendFormat:@"id:%@;name:%@\n", [[luckyNumbers objectAtIndex:i] objectForKey:@"id"], [[luckyNumbers objectAtIndex:i] objectForKey:@"name"]];


}


label.text = text;

}
<< objective-c对象retain和copy的区别iphone显示本地的图片【代码】 >>

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最近发表

Powered By Z-Blog 1.8 Walle Build 100427 Copyright 2011-2015 BuildApp.Net. All Rights Reserved.