0

iphoneアプリからサーバーからjsonにデータを解析していますが、jsonからデータを取得しません

私は次のコードを使用しています

jsonからデータを取得するには

これが私のjsonデータのリンクです

http://celeritas-solutions.com/emrapp/surveyDescription.php?user_id=ali40

 NSString*user=@"ali40";
 NSString *url=[NSString stringWithFormat:@"http://celeritas-solutions.com/emrapp/surveyDescription.php?user_id=%@",user];
 NSLog(url);
 NSArray *tempArray =[[DataManager staticVersion] startParsing:url];
 for (int i = 0; i<[tempArray count]; i++) {

    id *item = [tempArray objectAtIndex:i];
    NSDictionary *dict = (NSDictionary *) item;
    ObjectData *theObject =[[ObjectData alloc] init];
    [theObject setUser_id:[dict objectForKey:@"user_id"]];
    [theObject setSurvey_id:[dict objectForKey:@"survey_id"]];
    [theObject setSurvey_title:[dict objectForKey:@"survey_Title"]];
    [theObject setSurvey_Description:[dict objectForKey:@"survey_Description"]];    
    [theObject setDate_Created:[dict objectForKey:@"date_Created"]];
    [surveyList addObject:theObject];
    [theObject release];
    theObject=nil;
    int count =[surveyList count];
    NSLog(@"Total is %d",count);

DataManagerクラス

DataManager * theInstance;

  + (id)staticVersion{

if(!theInstance){
    theInstance = [[DataManager alloc] init];
}
return theInstance;
   }


  - (NSMutableArray *) startParsing:(NSString *)theURLString {

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",theURLString]];
NSString *fileContent= [NSString stringWithContentsOfURL:url];
SBJSON *parser = [[SBJSON alloc] init];  
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];  
NSArray *items = (NSArray *) data ;  


return items;

    int count=[items count];


    NSLog(@"This is testing %d",count);
    }
4

1 に答える 1

0

あなたのjsonは正しくありません。次のようになります。

{"ali40":[{"user_id":"ali40","survey_id":"1","survey_title":"Resturant Survey","survey_description":"Survey To get feedback from clients about food quality and any suggestion to improve the service","date_created":"2012-07-24 22:39:14","color":"[UIColor GrayColor]"},{"user_id":"ali40","survey_id":"2","survey_title":"Travel Servey","survey_description":"Toursim Survey","date_created":"2012-07-25 00:43:42","color":"[UIColor greyColor]"}]}

このコードはnullではない結果を返します

   NSString*user=@"ali40";
    NSString *url=[NSString stringWithFormat:@"http://celeritas-solutions.com/emrapp/surveyDescription.php?user_id=%@",user];
    NSLog(@"%@",url);
    NSData* data = [NSData dataWithContentsOfURL:
                    [NSURL URLWithString: url]];
    __autoreleasing NSError* error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error != nil) NSLog(@"%@",error);
    NSLog(@"%@",result);

出力:

2012-07-26 10:26:22.226 test[2511:f803] (
        {
        color = "[UIColor GrayColor]";
        "date_created" = "2012-07-24 22:39:14";
        "survey_description" = "Survey To get feedback from clients about food quality and any suggestion to improve the service";
        "survey_id" = 1;
        "survey_title" = "Resturant Survey";
        "user_id" = ali40;
    },
        {
        color = "[UIColor greyColor]";
        "date_created" = "2012-07-25 00:43:42";
        "survey_description" = "Toursim Survey";
        "survey_id" = 2;
        "survey_title" = "Travel Servey";
        "user_id" = ali40;
    }
)

jsonが多くのレコードを返す場合は、jsonファイルを変更する必要があります

 {"users":[{"user_id":"ali40","survey_id":"1","survey_title":"Resturant Survey","survey_description":"Survey To get feedback from clients about food quality and any suggestion to improve the service","date_created":"2012-07-24 22:39:14","color":"[UIColor GrayColor]"},{"user_id":"ali40","survey_id":"2","survey_title":"Travel Servey","survey_description":"Toursim Survey","date_created":"2012-07-25 00:43:42","color":"[UIColor greyColor]"}]} 

次に、レコードの配列を取得します。

 NSArray *allItems = [result objectForKey:@"users"];
    for (int i=0; i<allItems.count; ++i) {
        NSDictionary *item = [allItems objectAtIndex:i]; //here you get every user
        NSString *user_id=[item objectForKey:@"user_id"];
        NSLog(@"user_id %@",user_id);
    }
于 2012-07-26T07:50:14.453 に答える