gpt4 book ai didi

ios - 包含 NSString 数组的 JSON

转载 作者:行者123 更新时间:2023-11-29 02:48:16 25 4
gpt4 key购买 nike

如何从我实例化的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com