我是一名新程序员。我从服务器得到以下响应。如何从以下
获得0指数“Mile High Motors of Butte”和“Mile High Motors of Dillion”的价值由于
{
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;
}
答案 0 :(得分:2)
好的,让我们看看你的结构(假设你已经反序列化了你的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
}
答案 1 :(得分:0)
NSMutableArray yourStringArray= [[NSMutableArray alloc] init]; //for getting texts what you want.
NSArray * array1 = [yourDictionary valueForKey:@"dealer"];// You will get array which has dictionary elements.
for (NSDictionary *dealer in array1)// Write loop for getting your string.
{
NSString *dealerName = [dealer valueForKey:@"0"];
[yourStringArray addObject:dealerName];
}
我认为这会对你有所帮助。