代码之家  ›  专栏  ›  技术社区  ›  user6631314

IOS/Objective-C:如何访问括号内的JSON

  •  1
  • user6631314  · 技术社区  · 6 年前

    JSON spec .

    可以访问括号内API的以下JSON响应中的临时值,如下所示:

    JSON:
    main =     {
            humidity = 25;
            pressure = 1013;
            temp = "79.7";
            "temp_max" = "91.04000000000001";
            "temp_min" = "66.92";
        };
    
    NSNumber *temp = jsonResults[@"main"][@"temp"]; //returns the temp
    

    weather =     (
                    {
                description = "clear sky";
                icon = 01d;
                id = 800;
                main = Clear;
            }
        );
    

    我试过的都没用。提前谢谢你的建议。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Shehata Gamal    6 年前

    你可以试试

    NSArray*weather = jsonResults[@"weather"];
    NSString*main = weather[0][@"main"];
    
        2
  •  0
  •   humble_pie    6 年前
    NSString *main = [weather valueforkey:@"main"];