我是新来的程序员。我从服务器收到以下响应。我如何从以下获得0索引“巴特的英里高级电动机”和“百万的英里高级电动机”的值
谢谢
{
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
键是带有两个NSArray
的NSDictionaries
。因此,基于此我们可以做到:
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
}