iphone - 处理项目中的 JSON 响应

标签 iphone ios json

我是一名新程序员。我从服务器收到以下响应。我如何从下面获得 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(dealersuccess)。现在 dealer 键是一个带有两个 NSDictionariesNSArray。因此,基于此我们可以做:

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/

相关文章:

iphone - UIDatePicker - 问题本地化

iphone - 如何创建类似 Instagram 的应用内弹出警报

ios - 如何在 flutter 中设置 CupertinoDatePicker 小部件的最短时间?

javascript - Ember.js 将值绑定(bind)到 JSON 对象

json - 每 15 秒将单个消息合并为 1 个输出

java - @DateTimeFormat(pattern ="yyyy-MM-dd") 使用 Spring Mvc Rest Service 给出 "error 400 request syntactically incorrect"

iphone - 通用 iPhone/iPad AppDelegate

iphone - 裁剪图像 Objective-C

ios - 在我的 podspec 中设置 DEFINES_MODULE = YES 有什么缺点吗?

ios - 以编程方式更改 iOS UIBarButtonItem