在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;
}