-6

我有一个像这样的 JSON:-

 {    
    author =     {
            class = "pocketapptest.Author";
            dateCreated = "2013-05-08T00:03:46Z";
            firstName = Kurt;
            id = 1;
            lastName = Vonnegut;
            lastUpdated = "2013-05-08T00:03:46Z";
        };
        class = "pocketapptest.Book";
        dateCreated = "2013-05-08T00:03:46Z";
        id = 1;
        lastUpdated = "2013-05-08T00:03:46Z";
        title = "Slaughterhouse Five";
    }

我可以访问 dateceated / title 为

NSString* title = [jsondata objectForKey:@"title"];

但我如何存储名字/姓氏等。

4

4 回答 4

0

试试这样,你可以得到如下的值

 NSString* firstName = [[jsondata objectForKey:@"author"]objectForKey:@"firstName"];
于 2013-05-08T09:26:46.563 回答
0

我想你可能只使用jsondata objectForKey:两次。就像这样:

NSString* firstName = [[jsondata objectForKey:@"author"]objectForKey:@"firstName"];

希望能帮助到你

于 2013-05-08T09:27:12.597 回答
0

使用 ivars 创建一个类来存储来自 JSON 的数据

@interface MyDetails

@property(nonatomic, retain) NSString *class, *dateCreated, *firstName,*ID, *lastName, *lastUpdated;
@end

@implementation MyDetails

@synthesize class, dateCreated, firstName, ID, lastName, lastUpdated;

@end

为 mydetails 分配实例

 MyDetails *mydetails = [MyDetails alloc]init];

 mydetails.class = [[jsondata objectForKey:@"author"]objectForKey:@"class"];
 mydetails.dateCreated = [[jsondata objectForKey:@"author"]objectForKey:@"dateCreated"];
 mydetails.firstName = [[jsondata objectForKey:@"author"]objectForKey:@"firstName"];
 mydetails.ID = [[jsondata objectForKey:@"author"]objectForKey:@"id"];
 mydetails.lastName = [[jsondata objectForKey:@"author"]objectForKey:@"lastName"];
 mydetails.lastUpdated = [[jsondata objectForKey:@"author"]objectForKey:@"lastUpdated"];
于 2013-05-08T09:36:20.080 回答
0

JSON的无效。首先使用有效的 JSON您可以在此处检查您的 JSON 是否有效

然后从 json 访问字符串对象。

希望它可以帮助你。

于 2013-05-08T09:37:57.767 回答