Objective C 如何解析这个 NSString

Posted

技术标签:

【中文标题】Objective C 如何解析这个 NSString【英文标题】:Objective C how do I parse this NSString 【发布时间】:2014-06-24 21:51:41 【问题描述】:

我正在使用 Forecastr 包装库 (https://github.com/iwasrobbed/Forecastr) 从网站 Forecast.io 获取数据。目前在github项目中提供的示例项目中,输出吐出:


currently =     
    apparentTemperature = "68.72";
    cloudCover = 1;
    dewPoint = "66.43000000000001";
    humidity = "0.92";
    icon = cloudy;
    nearestStormDistance = 0;
    ozone = 327;
    precipIntensity = "0.0021";
    precipIntensityError = "0.0002";
    precipProbability = "0.01";
    precipType = rain;
    pressure = "1011.38";
    summary = "Humid and Overcast";
    temperature = "68.72";
    time = 1403645561;
    visibility = "4.62";
    windBearing = 182;
    windSpeed = "4.86";
;
daily =     
    data =         (
                    
            apparentTemperatureMax = "70.81999999999999";
            apparentTemperatureMaxTime = 1403658000;
            apparentTemperatureMin = "64.78";
            apparentTemperatureMinTime = 1403596800;
            cloudCover = "0.9399999999999999";
            dewPoint = "63.8";
            humidity = "0.86";
            icon = rain;
            moonPhase = "0.92";
            ozone = "328.74";
            precipIntensity = "0.0286";
            precipIntensityMax = "0.0834";
            precipIntensityMaxTime = 1403636400;
            precipProbability = 1;
            precipType = rain;
            pressure = "1013.38";
            summary = "Rain throughout the day.";
            sunriseTime = 1403600850;
            sunsetTime = 1403657295;
            temperatureMax = "70.81999999999999";
            temperatureMaxTime = 1403658000;
            temperatureMin = "64.78";
            temperatureMinTime = 1403596800;
            time = 1403582400;
            visibility = "7.24";
            windBearing = 169;
            windSpeed = "3.97";
        ,
                    
            apparentTemperatureMax = "75.17";
            apparentTemperatureMaxTime = 1403726400;
            apparentTemperatureMin = "65.19";
            apparentTemperatureMinTime = 1403694000;
            cloudCover = "0.99";
            dewPoint = "66.65000000000001";
            humidity = "0.89";
            icon = rain;
            moonPhase = "0.95";
            ozone = "323.68";
            precipIntensity = "0.0362";
            precipIntensityMax = "0.1194";
            precipIntensityMaxTime = 1403668800;
            precipProbability = "0.9399999999999999";
            precipType = rain;
            pressure = "1011.43";
            summary = "Rain in the morning and afternoon.";
            sunriseTime = 1403687270;
            sunsetTime = 1403743699;
            temperatureMax = "75.17";
            temperatureMaxTime = 1403726400;
            temperatureMin = "65.19";
            temperatureMinTime = 1403694000;
            time = 1403668800;
            visibility = "7.69";
            windBearing = 14;
            windSpeed = "5.46";
        ,
                    
            apparentTemperatureMax = "70.76000000000001";
            apparentTemperatureMaxTime = 1403805600;
            apparentTemperatureMin = "62.47";
            apparentTemperatureMinTime = 1403838000;
            cloudCover = "0.79";
            dewPoint = "62.15";
            humidity = "0.86";
            icon = rain;
            moonPhase = "0.98";
            ozone = "340.41";
            precipIntensity = "0.0134";
            precipIntensityMax = "0.0398";
            precipIntensityMaxTime = 1403762400;
            precipProbability = "0.91";
            precipType = rain;
            pressure = "1011.22";
            summary = "Light rain until afternoon.";
            sunriseTime = 1403773692;
            sunsetTime = 1403830101;
            temperatureMax = "70.76000000000001";
            temperatureMaxTime = 1403805600;
            temperatureMin = "62.47";
            temperatureMinTime = 1403838000;
            time = 1403755200;
            visibility = "9.67";
            windBearing = 4;
            windSpeed = "10.54";
        ,
                    
            apparentTemperatureMax = "77.7";
            apparentTemperatureMaxTime = 1403899200;
            apparentTemperatureMin = "56.96";
            apparentTemperatureMinTime = 1403856000;
            cloudCover = "0.15";
            dewPoint = "58.31";
            humidity = "0.73";
            icon = "partly-cloudy-day";
            moonPhase = "0.02";
            ozone = "354.38";
            precipIntensity = "0.0007";
            precipIntensityMax = "0.0021";
            precipIntensityMaxTime = 1403841600;
            precipProbability = "0.14";
            precipType = rain;
            pressure = "1016.58";
            summary = "Partly cloudy starting in the afternoon, continuing until evening.";
            sunriseTime = 1403860116;
            sunsetTime = 1403916500;
            temperatureMax = "77.7";
            temperatureMaxTime = 1403899200;
            temperatureMin = "56.96";
            temperatureMinTime = 1403856000;
            time = 1403841600;
            visibility = 10;
            windBearing = 332;
            windSpeed = "3.48";
        ,
                    
            apparentTemperatureMax = "82.34999999999999";
            apparentTemperatureMaxTime = 1403985600;
            apparentTemperatureMin = "58.31";
            apparentTemperatureMinTime = 1403946000;
            cloudCover = "0.14";
            dewPoint = "61.68";
            humidity = "0.76";
            icon = "partly-cloudy-day";

            moonPhase = "0.05";
            ozone = "346.75";
            precipIntensity = "0.0002";
            precipIntensityMax = "0.0004";
            precipIntensityMaxTime = 1403996400;
            precipProbability = "0.02";
            precipType = rain;
            pressure = "1020.75";
            summary = "Partly cloudy in the afternoon.";
            sunriseTime = 1403946543;
            sunsetTime = 1404002897;
            temperatureMax = "80.54000000000001";
            temperatureMaxTime = 1403985600;
            temperatureMin = "58.31";
            temperatureMinTime = 1403946000;
            time = 1403928000;
            windBearing = 230;
            windSpeed = "1.58";
        ,
                    
            apparentTemperatureMax = "87.03";
            apparentTemperatureMaxTime = 1404075600;
            apparentTemperatureMin = "60.66";
            apparentTemperatureMinTime = 1404028800;
            cloudCover = "0.4";
            dewPoint = "64.09999999999999";
            humidity = "0.75";
            icon = "partly-cloudy-day";
            moonPhase = "0.08";
            ozone = "320.44";
            precipIntensity = "0.0001";
            precipIntensityMax = "0.0002";
            precipIntensityMaxTime = 1404068400;
            precipProbability = "0.03";
            precipType = rain;
            pressure = "1019.06";
            summary = "Partly cloudy throughout the day.";
            sunriseTime = 1404032971;
            sunsetTime = 1404089291;
            temperatureMax = "83.62";
            temperatureMaxTime = 1404075600;
            temperatureMin = "60.66";
            temperatureMinTime = 1404028800;
            time = 1404014400;
            windBearing = 193;
            windSpeed = "2.76";
        ,
                    
            apparentTemperatureMax = "91.87";
            apparentTemperatureMaxTime = 1404158400;
            apparentTemperatureMin = "67.73999999999999";
            apparentTemperatureMinTime = 1404118800;
            cloudCover = "0.27";
            dewPoint = "69.93000000000001";
            humidity = "0.8";
            icon = "partly-cloudy-day";
            moonPhase = "0.11";
            ozone = "307.38";
            precipIntensity = "0.0013";
            precipIntensityMax = "0.0039";
            precipIntensityMaxTime = 1404180000;
            precipProbability = "0.29";
            precipType = rain;
            pressure = "1012.98";
            summary = "Partly cloudy starting in the afternoon.";
            sunriseTime = 1404119401;
            sunsetTime = 1404175684;
            temperatureMax = "85.01000000000001";
            temperatureMaxTime = 1404158400;
            temperatureMin = "67.73999999999999";
            temperatureMinTime = 1404118800;
            time = 1404100800;
            windBearing = 224;
            windSpeed = "5.24";
        ,
                    
            apparentTemperatureMax = "93.66";
            apparentTemperatureMaxTime = 1404244800;
            apparentTemperatureMin = "71.77";
            apparentTemperatureMinTime = 1404205200;
            cloudCover = "0.13";
            dewPoint = "73.33";
            humidity = "0.85";
            icon = rain;
            moonPhase = "0.13";
            ozone = "307.25";
            precipIntensity = "0.0095";
            precipIntensityMax = "0.0179";
            precipIntensityMaxTime = 1404237600;
            precipProbability = "0.64";
            precipType = rain;
            pressure = "1007.95";
            summary = "Light rain throughout the day.";
            sunriseTime = 1404205833;
            sunsetTime = 1404262073;
            temperatureMax = "84.91";
            temperatureMaxTime = 1404244800;
            temperatureMin = "71.77";
            temperatureMinTime = 1404205200;
            time = 1404187200;
            windBearing = 227;
            windSpeed = "5.82";
        
    );
    icon = rain;
    summary = "Rain throughout the week, with temperatures bottoming out at 71\U00b0F on Thursday.";
;
flags =     
    "darksky-stations" =         (
        KCXX
    );
    "isd-stations" =         (
        "711830-99999",
        "713710-99999",
        "713713-99999",
        "716120-99999",
        "716275-99999"
    );
    "madis-stations" =         (
        AS947,
        AU037,
        BATEU06,
        C3442,
        C7533,
        C8027,
        C8480,
        CWHM,
        CWIZ,
        CWTA,
        CWVQ,
        CYHU,
        CYUL,
        D2821,
        D3464,
        D8252
    );
    sources =         (
        nwspa,
        isd,
        "nearest-precip",
        gfs,
        fnmoc,
        sref,
        rtma,
        rap,
        nam,
        cmc,
        madis,
        darksky
    );
    units = us;
;
offset = "-4";
timezone = "America/Montreal";

现在库声明这是 JSON,看起来像 JSON,但实际上不是 JSON,所以我不确定如何在我的 Xcode 项目中解析这个 NSString。

解决方案: 它是一个 id 类型的 NSDictionary。因此,要将其解析为 mvanallen 状态,请执行以下操作:

NSLog(@"Summary: %@",[json valueForKeyPath:@"currently.summary"]);

..应该打印出来:

Summary: Humid and Overcast

仔细看代码,有一些externs,例如:

extern NSString *const kFCCurrentlyForecast;

因此我也可以这样做:

NSLog(@"%@",[[JSON objectForKey:kFCCurrentlyForecast] objectForKey:kFCApparentTemperature]);

【问题讨论】:

您似乎拥有的是一个代表一些 JSON 的 NSDictionary 的 NSLog(见第一个 字符?)。去 json.org 看看 JSON 语法(很容易学),然后明白 JSON 数组映射到 NSArray 对象,JSON“对象”映射到 NSDictionary 对象。两种表示形式的外观基本相同,只是数组的 JSON [] 替换为 NSArray 的 (),NSDictionary 键/值分隔符是 = 而不是 :。 (还有一些细微的差别。)所以 net-net 就是你所拥有的已经被解析了。 【参考方案1】:

这不是 JSON,这似乎是标准的 -description 被 NSLog'ed 的常规 NSDictionary。

如果是,那么一切都已经为你和这条日志语句解析好了..

NSLog(@"Summary: %@",[json valueForKeyPath:@"currently.summary"]);

..应该打印出来:

Summary: Humid and Overcast

【讨论】:

以上是关于Objective C 如何解析这个 NSString的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Objective C 中解析***的 JSON 数据?

如何在 Objective C 中解析 JSON 格式

如何解析 JSON 响应并在 Objective C 中使用它?

如何在Objective C中使用解析版本1.12.0加载PFQueryTableViewController中的对象?

如何解析数据,如果它是Objective c中的`isKindOfClass([NSString] class)`

如何在Objective-C中以编程方式发送带有一些正文文本的短信[重复]