gpt4 book ai didi

ios - 使用json,读取objective c中的内容

转载 作者:行者123 更新时间:2023-11-29 04:04:00 25 4
gpt4 key购买 nike

我正在学习一种从天气 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];

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

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

我想要的项目之一是“lateSTLoans”,这是所有数据显示的地方。所以我不知道如何获取其中一件元素

最佳答案

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

// 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"];

关于ios - 使用json,读取objective c中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15484608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com