SBJson int 解析
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想从 JSON 响应中获取整数值,您可以尝试此操作,
因为您的响应将在 nsstring 中。
you can try this if you want to get integer value from your JSON response
as your response would be in nsstring.
iOs 6.0,现在只是 [game[@"awayScore"] intValue]
iOs 6.0, now it's simply [game[@"awayScore"] intValue]
您必须首先将其作为 NSNumber 取出。
哎哟!
You have to pull it out as an NSNumber first.
Doh!