簡體   English   中英

如何從 Web 服務調用解釋 json

[英]how to interpret json from web service call

在我的 iOS 應用程序中,我成功收到了以下 json(我認為是 json):

{
    data =     {
        "current_condition" =         (
                        {
                cloudcover = 16;
                humidity = 59;
                "observation_time" = "09:09 PM";
                precipMM = "0.1";
                pressure = 1010;
                "temp_C" = 10;
                "temp_F" = 49;
                visibility = 10;
                weatherCode = 113;
                weatherDesc =                 (
                                        {
                        value = Clear;
                    }
                );
                weatherIconUrl =                 (
                                        {
                        value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png";
                    }
                );
                winddir16Point = NW;
                winddirDegree = 316;
                windspeedKmph = 47;
                windspeedMiles = 29;
            }
        );
        request =         (
                        {
                query = "Lat 32.35 and Lon 141.43";
                type = LatLon;
            }
        );
        weather =         (
                        {
                date = "2013-01-15";
                precipMM = "1.8";
                tempMaxC = 12;
                tempMaxF = 53;
                tempMinC = 10;
                tempMinF = 50;
                weatherCode = 119;
                weatherDesc =                 (
                                        {
                        value = Cloudy;
                    }
                );
                weatherIconUrl =                 (
                                        {
                        value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png";
                    }
                );
                winddir16Point = NNW;
                winddirDegree = 348;
                winddirection = NNW;
                windspeedKmph = 66;
                windspeedMiles = 41;
            },
                        {
                date = "2013-01-16";
                precipMM = "0.6";
                tempMaxC = 13;
                tempMaxF = 56;
                tempMinC = 11;
                tempMinF = 51;
                weatherCode = 113;
                weatherDesc =                 (
                                        {
                        value = Sunny;
                    }
                );

這里考慮的是什么數據? 它是json對象的根嗎? 目前狀況如何? 我想我需要解釋一下如何解釋這個 json,所以我可以在 tableview 中顯示它。 我試圖復習這個 json 教程,但里面的 json 完全不同! 它使用“:”來分隔鍵值,但是這個 json 沒有它。 我完全糊塗了

是的,這看起來像 JSON。 看看NSJSONSerialization類。 使用起來非常簡單。 您向JSONObjectWithData:options:error:方法傳遞一個包含您要讀取的 JSON 的NSData對象,它返回一個代表 JSON 數據“對象圖”的 Foundation Class 對象。 在您的情況下,頂級對象看起來像是一本字典,因此您應該取回 NSDictionary。

它看起來像 json。 您可以通過這種方式操作數據。

id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if ([greeting isKindOfClass:[NSDictionary class]]) {
    //manipulate dictionary
}else if ([greeting isKindOfClass:[NSArray class]]) {
//manipulate array
}else{
//manipulate other
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM