[英]JSON that contains an array to NSString
如何從實例化的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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.