0

我是一个新程序员。我从服务器收到以下响应。我如何从以下获得 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;
}
4

2 回答 2

2

好的,让我们看看你的结构(假设你已经反序列化了你的 JSON 字符串)。你有一个NSDictionary带有两个键(dealer& success)的。现在dealerkey 是 a NSArraywith two 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
}
于 2012-07-11T13:52:23.570 回答
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];
    }

I think it will be helpful to you.

于 2012-07-11T14:09:32.090 回答