IOS/Objective-C:将 JSON 转换为对象时出错
IOS/Objective-C: Error Converting JSON to Object
我正在尝试将 JSON 数组转换为对象以便在 table 中显示。我能够捕获 JSON。但是,当我尝试变成对象时,出现如下所示的错误。对象 class.
中肯定有一个名为 "name" 的 属性
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSMutableArray* latestItems = nil;
latestItems = [[NSMutableArray alloc] init];
latestItems = [json objectForKey:@"items"];
[self.tableView reloadData];
for (int i = 0; i < latestItems.count; i++)
{
NSDictionary *itemElement = latestItems[i];
// Create a new l object and set its props to todoElement properties
IDItemFromServer *newItem = [[IDItemFromServer alloc] init];
//ERROR NEXT LINE THROWS FOLLOWING ERROR
//'NSUnknownKeyException', reason: '[<IDItemFromServer 0x14e88010> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.'
[newItem setValue:@"hi" forKey:@"name"];
[newItem setValue:itemElement[@"address"] forKey:@"address"];
// Add this new item to the array
[latestItems addObject:newItem];
}
非常感谢有关如何修复错误的任何建议。
编辑:
//Object.h file
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface IDItemFromServer : NSObject
@property (nonatomic, retain) NSNumber * iid;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * address;
@end
它说 IDItemFromServer 没有 属性 name。添加 name,address 属性 就像下面的代码
In IDItemFromServer.h file
#import <Foundation/Foundation.h>
@interface IDItemFromServer : NSObject
@property(nonatomic,strong) NSString name;
@property(nonatomic,strong) NSString address;
@end
在 IDItemFromServer.m 文件中
#import "IDItemFromServer.h"
@implementation IDItemFromServer
@synthesize name;
@synthesize address;
@end
我正在尝试将 JSON 数组转换为对象以便在 table 中显示。我能够捕获 JSON。但是,当我尝试变成对象时,出现如下所示的错误。对象 class.
中肯定有一个名为 "name" 的 属性- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSMutableArray* latestItems = nil;
latestItems = [[NSMutableArray alloc] init];
latestItems = [json objectForKey:@"items"];
[self.tableView reloadData];
for (int i = 0; i < latestItems.count; i++)
{
NSDictionary *itemElement = latestItems[i];
// Create a new l object and set its props to todoElement properties
IDItemFromServer *newItem = [[IDItemFromServer alloc] init];
//ERROR NEXT LINE THROWS FOLLOWING ERROR
//'NSUnknownKeyException', reason: '[<IDItemFromServer 0x14e88010> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.'
[newItem setValue:@"hi" forKey:@"name"];
[newItem setValue:itemElement[@"address"] forKey:@"address"];
// Add this new item to the array
[latestItems addObject:newItem];
}
非常感谢有关如何修复错误的任何建议。
编辑:
//Object.h file
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface IDItemFromServer : NSObject
@property (nonatomic, retain) NSNumber * iid;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * address;
@end
它说 IDItemFromServer 没有 属性 name。添加 name,address 属性 就像下面的代码
In IDItemFromServer.h file
#import <Foundation/Foundation.h>
@interface IDItemFromServer : NSObject
@property(nonatomic,strong) NSString name;
@property(nonatomic,strong) NSString address;
@end
在 IDItemFromServer.m 文件中
#import "IDItemFromServer.h"
@implementation IDItemFromServer
@synthesize name;
@synthesize address;
@end