简体   繁体   English

如何在iPhone应用程序中从JSON数据获取整数值

[英]How to get a integer value from JSON data in iphone application

I am parisng data using following date which is in JSON 我正在使用JSON中的以下日期解析数据

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

Using 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];

    }

Book Class 书类

    @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;
}

that is because CatId is of tyoe NSString 那是因为CatId是类型NSString

Change it to NSNumber and try 将其更改为NSNumber并尝试

Hope this helps. 希望这可以帮助。

It seems that you are getting NSNumber when you parse the response for categoryId. 似乎在解析categoryId的响应时得到了NSNumber。 So try by taking NSNumber object inplace of NSString. 因此,尝试使用NSNumber对象代替NSString。

The dictionary you are getting stores numeric values as NSNumber objects probably. 您要获取的字典可能将数值存储为NSNumber对象。 So your catId should either be a NSNumber too , or -initWithDictionary: should extract the category id into a primitive type using eg -intValue and catId should then be declared as int . 因此,您的catId应该是NSNumber ,或者-initWithDictionary:应该使用-intValue将类别ID提取为原始类型,然后将catId声明为int

By the way, you should use -objectForKey: to get objects from NSDictionary s. 顺便说一句,您应该使用-objectForKey:NSDictionary获取对象。 It's marginally more performant since -valueForKey: does some extra processing, then calls -objectForKey: . 由于-valueForKey:一些额外的处理,然后调用-objectForKey:

Also json_string leaks and so robably does appDelegate.books . 此外json_string泄漏等robably做appDelegate.books

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM