Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用json数据_Iphone_Objective C_Ios_Xcode_Ios5 - Fatal编程技术网

Iphone 使用json数据

Iphone 使用json数据,iphone,objective-c,ios,xcode,ios5,Iphone,Objective C,Ios,Xcode,Ios5,我有以下代码解析从服务器接收的JSON数据: -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init]; NSArray *array_webdata=[[NSArray array] init]; NSString *searchStatus = [[NSString alloc] initWithDat

我有以下代码解析从服务器接收的JSON数据:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
NSArray *array_webdata=[[NSArray array] init];

NSString *searchStatus = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];    

array_webdata = [parsedata objectWithString:searchStatus error:nil];

NSDictionary *usersList = [array_webdata valueForKey:@"results"];
//我认为这不是真正的NSDictionary,因为如果我写NSArray*keys=[userslistallkeys]执行崩溃

NSLog(@"\n usersList =\n %@ \n", usersList);

[searchStatus release];
[connection release];
[webData release];
[pool drain];}
存储在usersList中的json数据具有以下结构:

(
{

  createTime = "date hour";

  fullname = "user name";

  "prof_id" = number;

   thumb = "image.jpg";

 },
 {
      data of rest of users...
 }
 )
我想创建一个类来存储每个用户的数据,并在我想使用特定用途时使用“prof_id”。 我需要这个,因为应用程序需要一个包含所有用户的列表(不是tableview),我认为这是最简单的方法


有人能帮我吗?谢谢

如果我没有错,你唯一需要做的就是:

NSMutableArray *yourArray = usersList;
然后是一个类似for的循环

for(int i = 0;i<[usersList count] ;i++)
{
NSMutableDictionary *yourDictionary = [usersList objectAtIndex:i];
int prof_id = [yourDictionary valueForKey:@"prof_id"];
}

for(int i=0;i如果我没有错,您需要做的唯一一件事是:

NSMutableArray *yourArray = usersList;
然后是一个类似for的循环

for(int i = 0;i<[usersList count] ;i++)
{
NSMutableDictionary *yourDictionary = [usersList objectAtIndex:i];
int prof_id = [yourDictionary valueForKey:@"prof_id"];
}
for(inti=0;i请使用框架解析从web服务接收的json数据

使用JSONKit读取数据并解析:

NSData* jsonData = [NSData dataWithData:webData];
JSONDecoder* decoder = [[JSONDecoder alloc]
                             initWithParseOptions:JKParseOptionNone];
NSArray* json = [decoder objectWithData:jsonData];
之后,必须使用for循环迭代json变量。
使用从NSObject类继承的名称
User
(file->new->file)创建新类,在.h/.m文件中创建所需参数。(进行合成以生成属性的getter/setter)

在连接类中导入User.h,在迭代器循环中创建用户实体的对象,并将这些对象添加到全局范围数组中

for(NSDictionary *userInfo in json) {
   User* user=[[User alloc] init];
   user.fullName=[userInfo valueForKey:@"fullname"];
   user.prof_id=[[userInfo valueForKey:@"prof_id"] integerValue];
   // Add into your global array
  [usersList addObject:user];
  [user release];// if ARC is not enable
}
// Check for successful object creation
NSLog(@"USER LIST contain User class Objects- %@",userList);
请使用框架解析从web服务接收的json数据

使用JSONKit读取数据并解析:

NSData* jsonData = [NSData dataWithData:webData];
JSONDecoder* decoder = [[JSONDecoder alloc]
                             initWithParseOptions:JKParseOptionNone];
NSArray* json = [decoder objectWithData:jsonData];
之后,必须使用for循环迭代json变量。
使用从NSObject类继承的名称
User
(file->new->file)创建新类,在.h/.m文件中创建所需参数。(进行合成以生成属性的getter/setter)

在连接类中导入User.h,在迭代器循环中创建用户实体的对象,并将这些对象添加到全局范围数组中

for(NSDictionary *userInfo in json) {
   User* user=[[User alloc] init];
   user.fullName=[userInfo valueForKey:@"fullname"];
   user.prof_id=[[userInfo valueForKey:@"prof_id"] integerValue];
   // Add into your global array
  [usersList addObject:user];
  [user release];// if ARC is not enable
}
// Check for successful object creation
NSLog(@"USER LIST contain User class Objects- %@",userList);

使用JSON框架,并使用下面的代码解析数据

NSString* newStr = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"yout link to json file"] encoding:NSUTF8StringEncoding error:nil];

NSLog(@"new str - %@",newStr);

NSArray *response = [newStr JSONValue];

NSLog(@"json array - %@",response);

使用响应数组显示结果。

使用JSON框架,并使用以下代码解析数据

NSString* newStr = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"yout link to json file"] encoding:NSUTF8StringEncoding error:nil];

NSLog(@"new str - %@",newStr);

NSArray *response = [newStr JSONValue];

NSLog(@"json array - %@",response);

使用响应数组显示结果。

我在解析json数据时没有问题,我的问题是我不知道如何创建一个类来存储这些数据并使用itI。我不确定我是否理解,user.h和user.m中会放什么代码?我一直在调查,在NSDictionary中,只有成对的值​​如果我理解正确,可以保存。我需要的是为每个用户保留四个数据,我认为NSDictionary是不正确的。对吗?你也可以在Dictionary中保存四个信息,用户作为键和值,它将存储这四个值和一个导入点的数组。请阅读苹果的objective c指南来处理OOP的concept,创建类,处理对象,这是非常关键的nano。谢谢你的帮助Mayur,我从这个世界开始,我发现即使是最简单的事情也很难做到;)我在解析json数据时没有问题,我的问题是我不知道如何创建一个类来存储这些数据并使用itI。我不确定我是否理解,user.h和user.m中会放什么代码?我一直在研究,在NSDictionary中,只有成对的值​​如果我理解正确,可以保存。我需要的是为每个用户保留四个数据,我认为NSDictionary是不正确的。对吗?你也可以在Dictionary中保存四个信息,用户作为键和值,它将存储这四个值和一个导入点的数组。请阅读苹果的objective c指南来处理OOP的concept,创建类,处理对象,这是非常重要的nano。谢谢你的帮助Mayur,我从这个世界开始,我发现即使是最简单的事情也很难做到;)如果我写NSArray*键=[usersList allKeys];为什么会崩溃???错误消息显示:[uuuu NSArray-allKeys]:未识别的选择器发送到实例0x6863270如果我写入NSArray*keys=[usersList-allKeys];为什么会崩溃???错误消息显示:[uuuu NSArrayM allKeys]:无法识别的选择器发送到实例0x6863270您只能在Dictionary中存储带有键的对象,而不能在float,int数据类型中存储。您只能在Dictionary中存储带有键的对象,不能在float,int数据类型中存储对象。