-1

我在这里解析 JSON 并正确获取数据。但是,当我尝试将信息放入 NSDictionary 并阅读时,我收到此错误

'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x1cda3d50'

这是代码

    if (data != nil) {

    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];

    NSDictionary *dic = [[res objectForKey:@"data"] objectForKey:@"current_condition"];
    self.location = [[dic objectForKey:@"request"]objectForKey:@"query"];
}

关于这里出了什么问题的任何想法?谢谢。

编辑

这是我登录 NSDictionary *res 时得到的

 data =     {
    "current_condition" =         (
                    {
            cloudcover = 100;
            humidity = 86;
            "observation_time" = "11:11 PM";
            precipMM = "2.2";
            pressure = 1019;
            "temp_C" = 1;
            "temp_F" = 34;
            visibility = 8;
            weatherCode = 296;
            weatherDesc =                 (
                                    {
                    value = "Light rain";
                }
            );
            weatherIconUrl =                 (
                                    {
                    value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0033_cloudy_with_light_rain_night.png";
                }
            );
            winddir16Point = SSW;
            winddirDegree = 210;
            windspeedKmph = 15;
            windspeedMiles = 9;
        }
    );
    request =         (
                    {
            query = "North Massapequa, United States Of America";
            type = City;
        }
    );
}
4

3 回答 3

1

您的 JSON 对象可能在您期望字典的某个位置包含一个数组(您发送objectForKey:到一个数组对象)。

尝试将 JSON 转储到某个地方,并确保它采用您期望的格式。

编辑:正如您从转储中看到的,res[@"request"]是一个数组(它有())。objectForKey:@"query"因此,您正在调用request数组并崩溃。

试试self.location = [[[res objectForKey:@"request"] objectAtIndex:0] objectForKey:@"query"];吧。

于 2013-01-28T23:10:20.420 回答
1

您误解了NSDictionary日志的输出,我认为这是一个长期存在的错误。

输出如下所示:

data =     {
    "current_condition" =         (
                    {
            cloudcover = 100;

这隐藏了数据的实际形状。看到数据、current_condition、weatherDesc 等行 = 之后的长时间中断了吗?那里应该有一个换行符,所以输出如下所示:

data =
{
    "current_condition" = 
    (
        {
            cloudcover = 100;

() 表示一个数组。(是的,即使 [] 在其他地方使用。这个输出是旧的和硬壳的,我怀疑 Apple 可以在不破坏代码的情况下修复它,即使他们突然认为它很重要。)

这显示了数据的真实形状:current_condition是一个数组。字典是数组中的第一个(索引 0)对象,而不是数组本身。

在整个数据中,这看起来像这样:

data =
{
    "current_condition" = 
    (
        {
            cloudcover = 100;
            humidity = 86;
            "observation_time" = "11:11 PM";
            precipMM = "2.2";
            pressure = 1019;
            "temp_C" = 1;
            "temp_F" = 34;
            visibility = 8;
            weatherCode = 296;
            weatherDesc =
            (
                {
                    value = "Light rain";
                }
            );
            weatherIconUrl =
            (
                {
                    value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0033_cloudy_with_light_rain_night.png";
                }
            );
            winddir16Point = SSW;
            winddirDegree = 210;
            windspeedKmph = 15;
            windspeedMiles = 9;
        }
    );
}

适当地调整你的代码以获得额外的间接级别,你应该没问题。

于 2013-01-28T23:43:57.453 回答
0

-[__NSCFArray objectForKey:]: unrecognized selector- 你可以在这里看到,很明显,你正在尝试调用objectForKey:数组。

这里:

self.location = [[dic objectForKey:@"request"]objectForKey:@"query"];

dic是一个数组:

data =     {
    "current_condition" =         ( // <--- array

可能你想做:

self.location = [[res objectForKey:@"request"]objectForKey:@"query"]; // res, not dic

request也是一个数组,你仍然会得到完全相同的错误......

于 2013-01-28T23:19:45.780 回答