gpt4 book ai didi

iphone - 问在xcode4中使用json

转载 作者:行者123 更新时间:2023-11-28 17:38:17 25 4
gpt4 key购买 nike

你好,我是 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 subway n 的 ViewController 现在我的问题是如何为选择数组获取 $id(参见 json 代码)如果在 php 中像这样评论

SELECT * FROM data where id="$id"

所以如果我选择列表 NIB,它们将在地铁 NIB 中显示输出

请帮帮我

谢谢

最佳答案

我也这样做,我使用 SBJSON 库:

https://github.com/stig/json-framework/

  • DownloadedData 是您从网络服务收到的 JSON
  • parsedData 是 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

关于iphone - 问在xcode4中使用json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9108664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com