0

我正在学习一种从天气 api 下载数据的非常基本的方法。基本上试图按照教程。

使用 URL,我可以将 JSON 格式的数据下载到字典中。然后放入数组中。

我现在的问题是如何读取数组中某个项目的特定值。

例如,当我对数组执行 NSLOG 时,我得到以下结果……我只剪切/粘贴了一对,因为有 55 个项目。

所以我的问题是如何获取这个数组的特定值?

2013-03-18 14:37:57.576 LocalWeatherV3[1220:c07] loans: {
    UV = 2;
    "dewpoint_c" = "-4";
    "dewpoint_f" = 24;
    "dewpoint_string" = "24 F (-4 C)";
    "display_location" =     {
        city = "Jersey City";
        country = US;
        "country_iso3166" = US;
        elevation = "47.00000000";
        full = "Jersey City, NJ";
        latitude = "40.75180435";
        longitude = "-74.05393982";
        state = NJ;
        "state_name" = "New Jersey";

        zip = 07097;
    };
    estimated =     {
    };
    "feelslike_c" = 2;
    "feelslike_f" = 35;
    "feelslike_string" = "35 F (2 C)";
    "forecast_url" = "http://www.wunderground.com/US/NJ/

这是 .m 的一部分

- (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseData //1

                          options:kNilOptions
                          error:&error];

    NSArray* latestLoans = [json objectForKey:@"current_observation"]; //2

    NSLog(@"loans: %@", latestLoans); //3

// 1) Get the latest loan
//NSDictionary* loan = [latestLoans objectAtIndex:1];
NSInteger counter = [latestLoans count];

提前致谢!!

所以当我这样做时

NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData //1

                      options:kNilOptions
                      error:&error];

我将鼠标悬停在本地手表上,我看到了

json    NSDictionary *  0x08d62d40
[0] key/value pair  
key id  0x08d61cf0
value   id  0x08d62100
[1] key/value pair  
key id  0x08d62150
value   id  0x08d633a0

然后我做

NSArray* latestLoans = [json objectForKey:@"current_observation"]; //2

NSLog(@"loans: %@", latestLoans); //3

我想要的项目之一是“最新贷款”,这是所有数据显示的地方。所以我不知道如何抓住其中一件物品

4

1 回答 1

1

假设您正在尝试获取预测网址。它很简单:

// update this line
NSDictionary *latestLoans = [json objectForKey:@"current_observation"];
// url variable will contain the first forecast url in the array
NSString *url = [latestLoans objectForKey:@"forecast_url"];
于 2013-03-18T19:01:06.380 回答