SBJson int 解析

发布于 2024-11-28 11:28:57 字数 2209 浏览 0 评论 0原文

我有一个 json 字符串:

{"1":{"homeTeam":"Home01","awayTeam":"Away01","homeScore":0,"awayScore":0,"gameID":1},"2":{"homeTeam":"Home11","awayTeam":"Away11","homeScore":0,"awayScore":0,"gameID":2},"3":{"homeTeam":"Home21","awayTeam":"Away21","homeScore":0,"awayScore":0,"gameID":3}}

我想将其转换为 Objective-C 类:

#import <Foundation/Foundation.h>


@interface ScoreKeeperGame : NSObject {

}

@property (nonatomic, retain) NSString *homeTeam;
@property (nonatomic, retain) NSString *awayTeam;
@property (nonatomic, assign) int homeScore;
@property (nonatomic, assign) int awayScore;
@property (nonatomic, assign) int gameID;

- (id) init: (NSString *) homeTeam 
   awayTeam: (NSString *) awayTeam;

- (id) init: (NSDictionary *) game;

@end

我通过 NSDictionary“game” 传入 json(json 字符串代表哈希图,因此第一个游戏是):

{"homeTeam":"Home01","awayTeam":"Away01","homeScore":0,"awayScore":0,"gameID":1}

当我尝试使用此时类:

#import "ScoreKeeperGame.h"


@implementation ScoreKeeperGame
@synthesize homeTeam=_homeTeam, awayTeam = _awayTeam, homeScore = _homeScore, awayScore     = _awayScore, gameID = _gameID;

- (id) init: (NSString *) homeTeam 
   awayTeam: (NSString *) awayTeam
{
    self = [super init];
    self.homeTeam = homeTeam;
    self.awayTeam = awayTeam;

NSLog(@"away: %@", awayTeam);
return self;
}

- (id) init: (NSDictionary *) game
{
    self = [super init];
    if(self)
    {
    self.homeTeam = [game objectForKey:@"homeTeam"];
    self.awayTeam = [game objectForKey:@"awayTeam"];
    self.awayScore = (int) [game objectForKey:@"awayScore"];
    self.gameID = [game objectForKey:@"gameID"];

    NSLog(@"game awayScore: %d", self.awayScore);
    NSLog(@"game gameID: %@", [NSNumber numberWithInt:self.gameID]);
    }
return self;
}

@end

awayScore 和gameId 被打印为大数字(也许是指针)?

我已经尝试过

self.awayScore = [NSNumber numberWithInt: [game objectForKey:@"awayScore"]];

但这似乎也不起作用。

如何从游戏对象中获取 int 值?

po game 产生:

{
awayScore = 0;
awayTeam = Away01;
gameID = 1;
homeScore = 0;
homeTeam = Home01;
}

谢谢!

I have a json string:

{"1":{"homeTeam":"Home01","awayTeam":"Away01","homeScore":0,"awayScore":0,"gameID":1},"2":{"homeTeam":"Home11","awayTeam":"Away11","homeScore":0,"awayScore":0,"gameID":2},"3":{"homeTeam":"Home21","awayTeam":"Away21","homeScore":0,"awayScore":0,"gameID":3}}

that I would like to turn into an Objective-C class:

#import <Foundation/Foundation.h>


@interface ScoreKeeperGame : NSObject {

}

@property (nonatomic, retain) NSString *homeTeam;
@property (nonatomic, retain) NSString *awayTeam;
@property (nonatomic, assign) int homeScore;
@property (nonatomic, assign) int awayScore;
@property (nonatomic, assign) int gameID;

- (id) init: (NSString *) homeTeam 
   awayTeam: (NSString *) awayTeam;

- (id) init: (NSDictionary *) game;

@end

I pass the json in by NSDictionary "game" (the json string represents a hashmap, so the first game is):

{"homeTeam":"Home01","awayTeam":"Away01","homeScore":0,"awayScore":0,"gameID":1}

When I try to use this class:

#import "ScoreKeeperGame.h"


@implementation ScoreKeeperGame
@synthesize homeTeam=_homeTeam, awayTeam = _awayTeam, homeScore = _homeScore, awayScore     = _awayScore, gameID = _gameID;

- (id) init: (NSString *) homeTeam 
   awayTeam: (NSString *) awayTeam
{
    self = [super init];
    self.homeTeam = homeTeam;
    self.awayTeam = awayTeam;

NSLog(@"away: %@", awayTeam);
return self;
}

- (id) init: (NSDictionary *) game
{
    self = [super init];
    if(self)
    {
    self.homeTeam = [game objectForKey:@"homeTeam"];
    self.awayTeam = [game objectForKey:@"awayTeam"];
    self.awayScore = (int) [game objectForKey:@"awayScore"];
    self.gameID = [game objectForKey:@"gameID"];

    NSLog(@"game awayScore: %d", self.awayScore);
    NSLog(@"game gameID: %@", [NSNumber numberWithInt:self.gameID]);
    }
return self;
}

@end

The awayScore and gameId are printed as large numbers (maybe pointers)?

I've tried

self.awayScore = [NSNumber numberWithInt: [game objectForKey:@"awayScore"]];

But that didn't seem to work either.

How do I get the value of the int from the game object?

po game produces:

{
awayScore = 0;
awayTeam = Away01;
gameID = 1;
homeScore = 0;
homeTeam = Home01;
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅笑依然 2024-12-05 11:28:58

如果您想从 JSON 响应中获取整数值,您可以尝试此操作,

self.awayScore = [NSNumber numberWithInt: [[game objectForKey:@"awayScore"] intValue]];

因为您的响应将在 nsstring 中。

you can try this if you want to get integer value from your JSON response

self.awayScore = [NSNumber numberWithInt: [[game objectForKey:@"awayScore"] intValue]];

as your response would be in nsstring.

故人如初 2024-12-05 11:28:58

iOs 6.0,现在只是 [game[@"awayScore"] intValue]

iOs 6.0, now it's simply [game[@"awayScore"] intValue]

萧瑟寒风 2024-12-05 11:28:57

您必须首先将其作为 NSNumber 取出。

NSNumber *myNum = [game objectForKey:@"awayScore"];

self.awayScore = [myNum intValue];

哎哟!

You have to pull it out as an NSNumber first.

NSNumber *myNum = [game objectForKey:@"awayScore"];

self.awayScore = [myNum intValue];

Doh!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文