如何从我实例化的 NSDictionary 中提取每个地址(如下),以便我可以分配给各种 NSString 对象?
我有以下 JSON:
{
"Address":[
{
"$":{
"ID":"0"
},
"Address2":[
"10 Smith RD"
],
"City":[
"Mapleville"
],
"State":[
"NJ"
],
"Zip5":[
"90210"
],
"Zip4":[
"764"
]
},
{
"$":{
"ID":"1"
},
"Address2":[
"32 Hog CT"
],
"City":[
"New York City"
],
"State":[
"NY"
],
"Zip5":[
"90210"
],
"Zip4":[
"1390"
]
}
]
}
cData(如下)来自上面的 JSON。 我执行了以下操作来转换为 NSDictionary:
NSDictionary* dictionary2 = [NSJSONSerialization JSONObjectWithData:cData options:NSJSONReadingAllowFragments error:nil];
一旦我做了:
NSLog(@"%@", dictionary2);
NSLog 的输出:
{
Address = (
{
"$" = {
ID = 0;
};
Address2 = (
"10 Smith RD"
);
City = (
"Mapleville"
);
State = (
NJ
);
Zip4 = (
7642
);
Zip5 = (
90210
);
},
{
"$" = {
ID = 1;
};
Address2 = (
"32 Hog CT"
);
City = (
"New York City"
);
State = (
NY
);
Zip4 = (
1390
);
Zip5 = (
90210
);
}
);
}
最佳答案
只要遵循结构即可。
NSDictionary* dictionary2 = [NSJSONSerialization ...
NSArray *addresses = dictionary2[@"Address"];
for (NSDictionary *addressData in addresses) {
NSString *address2 = addressData[@"Address2"];
NSString *city = addressData[@"City"];
// and the rest as needed
}
关于ios - 包含 NSString 数组的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24845777/