询问在 xcode4 中使用 json
Posted
技术标签:
【中文标题】询问在 xcode4 中使用 json【英文标题】:ask using json in xcode4 【发布时间】:2012-02-02 07:37:15 【问题描述】:您好,我是 iPhone 开发新手 我想在 xcode4 中将带有 json 的选择数组显示到 mysql 我在 php 中输出 json 代码,如下所示:
"data":["id":"16","nama":"yes","desk":"test2","gambar":""]
我的代码模块是这样的:
list.m
- (void)viewDidLoad
AIR = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:@"http://192.168.0.169/demo/json/rifle.php"];
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];
NSLog(jsonreturn);
NSData *jsonData =[jsonreturn dataUsingEncoding:NSUTF8StringEncoding];
NSError *error =nil;
NSDictionary *dict =[[CJSONDeserializer deserializer]deserializeAsDictionary:jsonData error:&error];
if (dict)
AIR = [[dict objectForKey:@"data"] retain];
NSLog(@"Array:%@",AIR);
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
// Return the number of sections.
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
// Return the number of rows in the section.
return [AIR count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
// Configure the cell...
NSDictionary *dict = [AIR objectAtIndex:indexPath.row];
// deklarasi image ambil file gambar didalam field nama
cell.textLabel.text = [dict objectForKey:@"nama"];
// deklarasi image ambil file gambar didalam field desk
cell.detailTextLabel.text = [dict objectForKey:@"desk"];
// deklarasi image ambil file gambar didalam field gambar
UIImage *cellImage =[UIImage imageNamed:[dict objectForKey:@"gambar"]];
cell.imageView.image = cellImage;
return cell;
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
subway *mAIR = [[subway alloc] initWithNibName:@"subway" bundle:nil];
[mAIR setTitle:@"AIRGUN DESC DETAIL"];
[self.navigationController pushViewController:mAIR animated:YES];
[mAIR release];
subway 是另一个带有 tableView iniNibName 地铁 n 的 ViewController 现在我的问题是如何获取选择数组的 $id(参见 json 代码) 如果在 php 中喜欢这个评论
SELECT * FROM data where id="$id"
所以如果我选择列表NIB,它们将在地铁NIB中显示输出
请帮帮我
感谢之前
【问题讨论】:
仅供参考,请尝试使用 JSONKit 解析器 (github.com/johnezang/JSONKit),因为它的速度非常快... 很抱歉,我无法解析您的问题。请尝试澄清它。 这是一个示例 blackberrymastercracks.blogspot.in/2012/08/…xcode 中的 JSON。 【参考方案1】:我也这样做,我使用 SBJSON 库:
https://github.com/stig/json-framework/
DownloadedData 是您从网络服务收到的 JSONparsedData 是解析 JSON 的 NSDicitonary
NSString *responseString = [[NSString alloc] initWithData:downloadedData encoding:NSUTF8StringEncoding];
SBJSON *parser = [SBJSON new];
NSDictionary *parsedData = (NSDictionary *)[parser objectWithString:responseString error: nil];
[responseString release];
[parser release];
您可以在此处找到教程: http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c
【讨论】:
我可以得到关于安装 sbjson 的参考 试试这个:blog.zachwaugh.com/post/309924609/… 我仍然无法理解......关于我只想在我请求192.168.0.169/demo/json/rifle.php?id=%@时才想这样使用的参考【参考方案2】:使用 JSONKit https://github.com/johnezang/JSONKit
您只需要 2 个文件(JSONKit.h 和 JSONKit.m) - 将其复制到您的项目中 然后你需要将它导入到你的 .m 文件中:
#import "JSONKit.h"
根据需要将数据下载到 NSString *jsonString,然后使用:
NSArray *jsonArray = [jsonString objectFromJSONString];
这个数组你得到你的响应,现在使用 NSDictionary 或 NSArray 来获取变量(取决于 json 响应中的数据) - NSDictionary 是 key:value 而 NSArray 只是对象数组。
因此,要从您的 json 响应中获取您的 ID = 16,您需要以下内容:
NSLog(@"My ID is %@", [[jsonArray objectAtIndex:0] objectForKey:@"id"]);
您可以随时查看 NSDictionary 或 NSArray 的内容,例如 NSLog(@"%@", jsonArray);
或者如果“数据”部分是字典,使用 [[jsonDictionary objectForKey:@"data"] objectForKey:@"id"] 因为你有字典,里面是另一个字典结构的响应。
【讨论】:
这与他的 CJSONDeserializer 可能在做什么有什么不同?【参考方案3】:这里是最简单的json解析方法,希望对你有所帮助
ViewController.h
@interface ViewController : UIViewController <NSURLConnectionDelegate>
NSArray *ids;
NSArray *nama;
NSArray *desk;
NSArray *gambar;
//=======Json Variable
NSMutableData *webdata;
NSURLConnection *conn;
//======
ViewController.m
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *urlstring=[NSString stringWithFormat:@"http://192.168.0.169/demo/json/rifle.php"];
NSURL *url=[NSURL URLWithString:urlstring];
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] initWithURL:url];
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
webdata = [[NSMutableData alloc] init];
#pragma NSUrl Connection Delegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
[webdata setLength:0];
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
[webdata appendData:data];
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
NSString *str=[[NSString alloc]initWithBytes:[webdata mutableBytes] length:[webdata length] encoding:NSUTF8StringEncoding];
NSLog(@"Data Length %d",[webdata length]);
NSMutableDictionary *result = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
ids = [[result valueForKey:@"data"] valueForKey:@"id"];
nama = [[result valueForKey:@"data"] valueForKey:@"nama"];
desk = [[result valueForKey:@"data"] valueForKey:@"desk"];
gambar = [[result valueForKey:@"data"] valueForKey:@"gambar"];
【讨论】:
以上是关于询问在 xcode4 中使用 json的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 libpqxx 和 xcode4 (c++) 进行编译