我脑子里乱七八糟。枚举、NSObject、自定义接口(interface)等等... 我只需要在 Objective C 中声明数据类型来表示这个 JSON 集合:
{
"firstName": "John",
"lastName" : "Smith",
"age" : 25,
"address" :
{
"streetAddress": "21 2nd Street",
"city" : "New York",
"state" : "NY",
"postalCode" : "10021"
},
"phoneNumber":
[
{
"type" : "home",
"number": "212 555-1234"
},
{
"type" : "fax",
"number": "646 555-4567"
}
]
这是维基百科 JSON 文章中的示例。所以我需要解释一下如何将所有这些数据放入集合中(或自定义类型或其他我不知道的东西)。
例如,我需要 1000 个“电话簿”集合实例来存储和更改 1000 个人的信息。我需要知道我该怎么做。
最佳答案
名字和地址示例:
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
NSString *firstname = [json objectForKey:@"firstname"];
NSDictionary *address = [json objectForKey:@"address"];
关于objective-c - 我如何在 Objective C 中表示当前的 JSON 方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8483010/