gpt4 book ai didi

php - PHP 和 Objective-C 之间的 JSON 对象

转载 作者:搜寻专家 更新时间:2023-10-30 20:23:03 27 4
gpt4 key购买 nike

我的 iPhone 应用程序正在从 PHP 网站抓取一个 JSON 对象。

PHP 站点上的对象具有多个整数属性。

从数据库中检索的属性(它们存储为 INT)在 JSON 中看起来像这样:"seatedplayers":"3"

在 PHP 代码中修改的属性在 JSON 中看起来像这样:"currentseat":1

在我的 Obj-c 中,我使用 NSNumberFormatter 来正确格式化它们,如下所示:
[numFormatter numberFromString:[jsonObject objectForKey:@"seatedplayers"]];(jsonObject 是一个 NSDictionary)

该代码适用于在值周围使用引号的属性,但不适用于其他属性。如果我在我的 Objective-C 代码中添加 stringValue,它会在不带引号的属性上运行,但不能在带引号的属性上运行。

现在,我的大问题是,这个应用程序需要与我不会亲自编写的站点通信,那么我可以在 Objective-C 中编写什么代码来处理这两个实例?

编辑
这就是我获取 NSDictionary jsonObject 的方式。

- (NSDictionary *) getJSONObjectFromWebAddress:(NSString *) address {
// Prepare the URL Request
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:address]];

// Perform the request, expecting JSON back
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

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

NSError *error;
SBJsonParser *parser = [[SBJsonParser new] autorelease];
NSDictionary *jsonObject = [parser objectWithString:responseString error:&error];
if (!jsonObject) {
NSLog(@"%@", error);
}

[responseString release];
return jsonObject;
}

最佳答案

我遇到了同样的问题。幸运的是,NSString 和 NSNumber 都响应 intValue,因此您可以安全地使用它。


//NSDictionary* myJsonParsed;
[NSString stringWithFormat:@"%d", [[myJsonParsed valueForKey:@"stringOrInt"] intValue]];

关于php - PHP 和 Objective-C 之间的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4834495/

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