我是一名新程序员。我从服务器收到以下响应。我如何从下面获得 0 索引“Butte 的 Mile High Motors”和“Dillion 的 Mile High Motors”的值
谢谢
{
dealer = (
{
0 = "Mile High Motors of Butte";
1 = "3883 Harrison";
2 = Butte;
3 = 59701;
4 = MT;
5 = "http://www.buttesmilehighchryslerjeepdodge.com";
6 = 2;
7 = 0;
address = "3883 Harrison";
city = Butte;
distance = 0;
id = 2;
name = "Mile High Motors of Butte";
state = MT;
url = "http://www.buttesmilehighchryslerjeepdodge.com";
zip = 59701;
},
{
0 = "Mile High Motors of Dillon";
1 = "790 N Montana St";
2 = Dillon;
3 = 59725;
4 = Montana;
5 = "http://www.MileHighDillon.com";
6 = 13;
7 = "60.1235269593172";
address = "790 N Montana St";
city = Dillon;
distance = "60.1235269593172";
id = 13;
name = "Mile High Motors of Dillon";
state = Montana;
url = "http://www.MileHighDillon.com";
zip = 59725;
}
);
success = 1;
}
最佳答案
好的,让我们看看您的结构(假设您已经反序列化了 JSON 字符串)。
您有一个带有两个键的 NSDictionary
(dealer
和 success
)。现在 dealer
键是一个带有两个 NSDictionaries
的 NSArray
。因此,基于此我们可以做:
NSDictionary *myJson; // Assuming that this is what you have posted
NSArray *dealers = [myJson valueForKey:@"dealer"];
// Now just grab whatever you need
NSString *dealerOne = [[dealers objectAtIndex:0] valueForKey:@"0"]; //Mile High Motors of Butte
NSString *dealerTwo = [[dealers objectAtIndex:1] valueForKey:@"0"]; //Mile High Motors of Dillon
或者您可以像这样迭代您的 dealers
数组:
for (NSDictionary *dealer in dealers)
{
NSString *dealerName = [dealer valueForKey:@"0"];
// Do something useful
}
关于iphone - 处理项目中的 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11434007/