mysql - SBJsonParser 将来自服务器的响应存储在自定义类中

标签 mysql ios arrays json class

我有一个 mySQL 数据库和一个 php 脚本,该脚本从所述数据库返回表的内容。我将此数据作为字符串,并且能够将其放入数组中,但我不太确定如何将其放入自定义 NSObject 类中。我有什么办法可以做到这一点吗?

最佳答案

如果您的字符串是 JSON 对象数组,您可以使用 SBJsonParser 方法 objectWithString: 它将返回 NSDictionary 对象的 NSArray。然后,您可以循环遍历数组并从数组中的每个元素创建一个自定义对象。使用 NSDictionary 方法 objectForKey: 提取每个值。

SBJsonParser *json = [[SBJsonParser new] autorelease];
NSError *jsonError = nil;
NSArray *parsedJSON = [json objectWithString:response error:&jsonError];            

// this example just creates a custom object from the first element in the array
NSDictionary dictionary = [parsedJSON objectAtIndex:0]           
CustomObject *myCustomObject = [[CustomObject alloc] init];
myCustomObject.firstname = [dictionary objectForKey:@"firstname"];
myCustomObject.lastname = [dictionary objectForKey:@"lastname"];

关于mysql - SBJsonParser 将来自服务器的响应存储在自定义类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8782332/

相关文章:

java - 使用 Streams 将 3 个数组转换为 1 个对象数组

c - 随机选择名字时出现奇怪的字符

mysql - 从备份中恢复 Magento 静态 block 内容?

ios - 允许 SpriteKit 中的触摸传播

mysql - 解释命令中 select_type 列中的 DEPENDENT UNION 是什么?

iphone - ( iOS ) 什么是 superview 什么是 subviews

ios - 以编程方式读取 Xcode 项目的build设置

javascript - 单击按钮时将 div 元素添加到数组

PHP:PDO foreach 不工作

php - 在 cookie 中为我的每个未登录用户提供唯一的 session ID