2
我正在使用Forecastr包裝庫(https://github.com/iwasrobbed/Forecastr)從Forecast.io網站獲取數據。目前在GitHub的項目中提供的示例項目,輸出吐出:Objective C如何解析此NSString
{
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。
SOLUTION:
它是一個類型爲id的NSDictionary。因此,要分析它作爲mvanallen狀態做到這一點:
NSLog(@"Summary: %@",[json valueForKeyPath:@"currently.summary"]);
..should打印出:
Summary: Humid and Overcast
看代碼接近有一定的實習醫生如:
extern NSString *const kFCCurrentlyForecast;
因此我也可以這樣做:
NSLog(@"%@",[[JSON objectForKey:kFCCurrentlyForecast] objectForKey:kFCApparentTemperature]);
你似乎有什麼的一個的NSLog NSDictionary(請參閱代表某個JSON的第一個「{」字符?)。轉到json.org以查看JSON語法(學習起來非常簡單),然後瞭解JSON數組映射到NSArray對象,並將JSON「對象」映射到NSDictionary對象。在兩種表示中,外觀大多相同,只有數組的JSON'[]'被替換爲NSArray的'()',而NSDictionary鍵/值分隔符是'='而不是':'。 (還有一些細微的差別。)所以net-net就是你已經解析了。 –