2011-08-25 2 views
1

Je suis données parisng par date suivante qui est en JSONComment obtenir une valeur entière à partir des données JSON en application iPhone

[ 
{ 
    "categoryId": 202, 
    "name": "Sport" 
}, 
{ 
    "categoryId": 320, 
    "name": "Fritid" 
}, 
{ 
    "categoryId": 350, 
    "name": "Kultur" 
}, 
{ 
    "categoryId": 4920, 
    "name": "Diverse" 
}, 
{ 
    "categoryId": 4774, 
    "name": "Samfunn" 
} ] 

Utilisation follwing code

SBJsonParser *parser = [[SBJsonParser alloc] init]; 

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?method=categories&appid=620&mainonly=true"]]; 
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 
NSDictionary *object = [parser objectWithString:json_string error:nil]; 
//appDelegate.books = [[NSMutableArray alloc] initWithCapacity:0]; 

appDelegate.books = [[NSMutableArray alloc] init]; 

NSArray *results = [parser objectWithString:json_string error:nil]; 
for (int i=0; i<[results count]; i++) { 
    Book *aBook = [[Book alloc] initWithDictionary:[results objectAtIndex:i]]; 
    [appDelegate.books addObject:aBook]; 


    [aBook release]; 

    } 

Livre Classe

@interface Book : NSObject { 


NSString *catId; 


NSString *name; 

    } 

    @property(nonatomic,retain)NSString*catId; 

    @property(nonatomic,retain) NSString *name; 

    @end 


    #import "Book.h" 


@implementation Book 


@synthesize catId,name; 

-(id)init{ 
    self=[super init]; 
} 

- (id)initWithDictionary:(NSDictionary*) dict { 
    self.catId = [dict valueForKey:@"categoryId"]; self.name = 
    [dict valueForKey:@"name"]; 
    return self; 
} 
+0

ma question est que je ne reçois pas de valeur catid – ali

+0

j'ai donné le code de initWithDict – ali

+0

catid montre son pas CFSting – ali

Répondre

6

qui est parce que CatId est de tyoe NSString

Modifier à NSNumber et essayer

Hope this helps.

+0

si j'utilise NSInteger il donne cette valeur dans catid 79765008 – ali

+0

c'est parce qu'il donne l'adresse de la variable pointée par cette variable – Robin

+0

utilise la méthode intValue sur vous variable nsinteger – Robin

2

Il semble que vous obtenez NSNumber lorsque vous analysez la réponse pour categoryId. Essayez donc en prenant l'objet NSNumber à la place de NSString.

+0

pouvez-vous aider à faire comme ça que j'ai fait mais pas réussi – ali

0

Le dictionnaire que vous obtenez stocke des valeurs numériques comme des objets NSNumber probablement. Donc, votre catId devrait être soit un NSNumber trop ou -initWithDictionary: doit extraire l'ID de catégorie dans un type primitif en utilisant par exemple -intValue et catId doit alors être déclaré int.

Par ailleurs, vous devez utiliser -objectForKey: pour obtenir des objets de NSDictionary s. Il est légèrement plus performant puisque -valueForKey: fait un traitement supplémentaire, puis appelle -objectForKey:.

Egalement json_string fuit et donc appDelegate.books.

Questions connexes