【问题标题】:Get a single specific value from json从 json 获取单个特定值
【发布时间】:2014-05-15 21:07:13
【问题描述】:

正如您所料,我对 obj-C 还很陌生,而且我一直在努力积累知识和经验。但我仍在为很多概念而苦苦挣扎,其中包括 JSON 数据“捕获”。 我看过很多教程和指南,但我无法将它们翻译成我需要的东西。大多数时候,他们在数组中布局数据或获取多个值,并且(当然)使用不同的变量,这让我感到困惑和不清楚,尽管这应该是愚蠢的简单。

我正在尝试做一些非常简单的事情: 从开放天气 API 中获取单个值,即温度。

我会向你展示我的代码,根据我可耻的知识,它应该是完美的,但显然它不起作用:D

@implementation HomeViewController
{

    NSMutableArray *tableData;
    NSDictionary *jsonDict;
    NSMutableString *title;
}
-(void) viewDidLoad
{
    [super viewDidLoad];

    NSError *error;


    //I create my data array and the string i'll store my value later on
    tableData = [[NSMutableArray alloc] init];
    title = [[NSMutableString alloc]init];

   // Creating the link for the json api so it fits coordinates ; this works but i edited the locations out to clear the code
    NSString *s = [[NSString alloc]initWithFormat:@"http://api.openweathermap.org/data/2.5/weather?lat=%.05f&lon=%.05f", _annotation.coordinate.latitude, _annotation.coordinate.longitude];

    // I go online and catch the data of the url stored in S
    NSData *jSonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:s]];

    // This is a dictionary where all my data is stored from jsonData, keys and values all the way
    jsonDict = [NSJSONSerialization JSONObjectWithData:jSonData options:NSJSONReadingMutableContainers error:&error];

    // I use the string created previously and assign it the value stored in that dictionary, in the TEMP 'folder', right under MAIN.

    title = [[jsonDict objectForKey:@"main"]objectForKey:@"main.temp"];

     // I assign that title to a label so it appears in my view.
     self.tempLabel.text = title;
    ...
    }

给你。我可能遗漏了一些非常简单的东西,但我一直坚持这一点,即使我觉得我知道自己在做什么,我也可能遗漏了一些东西。所以如果你给我的答案,你也可以告诉我我做错了什么:D

非常感谢您的支持和知识。这个社区太棒了:)

【问题讨论】:

  • NSJSONSerialization 行之后放置一个NSLog( @"%@", jsonDict );,并将输出添加到您的问题中。这将使人们更容易帮助您解决这个问题。

标签: ios json key-value weather-api


【解决方案1】:

jsonDict赋值后放一个断点并使用

po jsonDict

在控制台中打印出你得到的东西。然后,调整提取值的代码。并使用现代的 Objective-C 语法。

示例

title = jsonDict[@"main"][@"temp"];

注意

po 是一个调试器命令,它将打印出对象的内容。如果您需要打印出原语的内容,请改用p

【讨论】:

  • 如果你能告诉我“po”是什么,我很乐意尝试:D
  • po 是一个调试器命令,将打印出对象的内容。
【解决方案2】:

我的猜测是

 jsonDict = [NSJSONSerialization JSONObjectWithData:jSonData options:NSJSONReadingMutableContainers error:&error];

正在尝试创建一个 nsdictionary,但结果以数组的形式返回。试试这个:

NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonDict options: NSJSONReadingMutableContainers error: &e];

if (!jsonArray) {
  NSLog(@"Error parsing JSON: %@", e);
} else {
   for(NSDictionary *item in jsonArray) {
      NSLog(@"Item: %@", item);
   }
}

【讨论】:

    【解决方案3】:

    这应该会让你正确:

    title = [[jsonDict objectForKey:@"main"]objectForKey:@"temp"];

    为了解释这个问题,您似乎是指 temp 在键中使用点语法的组合。

    编辑:针对您的错误:

    当您尝试在非 NSString 类型的值上查找字符串的长度时,会出现该错误。看起来temp 正在以数字形式返回。因此,要执行您想要执行的操作,您需要将 [[jsonDict objectForKey:@"main"]objectForKey:@"temp"] 转换为 NSString:

    NSNumber *temp = [[jsonDict objectForKey:@"main"]objectForKey:@"temp"]; NSString *tempString = [temp stringValue];

    NSString *temp = [[[jsonDict objectForKey:@"main"]objectForKey:@"temp"] stringValue];

    这将使您获得lengthtemp.length

    **编辑:除非您尝试获取天气数据数组的长度...在这种情况下,我希望看到更多该代码

    【讨论】:

    • 我也试过这个,但是当我到达我应该看到温度的页面时,它会使应用程序崩溃;这是我得到的错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFNumber 长度]:无法识别的选择器发送到实例 0x19e3ad30”
    猜你喜欢
    • 1970-01-01
    • 2019-03-30
    • 2013-08-20
    • 2017-11-22
    • 2017-09-19
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多