• NSJSONSerialization解析网络天气数据


    NSJSONSerialization

    1.IOS中Json解析的四种方法
        参考 enuola
    2.代码示例
     1 -(void) testNSJSONSerialization
     2 {
     3     // define error
     4     NSError *error;
     5     // define url request
     6     NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString: @"http://m.weather.com.cn/data/101010100.html"]];
     7     // get reponse data from url request
     8     NSData *dataResponse = [NSURLConnection sendSynchronousRequest: urlRequest returningResponse: nil error: nil];
     9     // use nsjson transfer data to dictionary
    10     NSDictionary *dictionaryWeather = [NSJSONSerialization JSONObjectWithData: dataResponse options: NSJSONReadingMutableLeaves error: &error];
    11     // get weather info from weather dictionary
    12     NSDictionary *dictionaryWeatherInfo = [dictionaryWeather objectForKey: @"weatherinfo"];
    13     // output weather of today
    14     NSLog(@"今天是 %@  %@  %@  的天气状况是:%@  %@ ", [dictionaryWeatherInfo objectForKey: @"date_y"], [dictionaryWeatherInfo objectForKey: @"week"],[dictionaryWeatherInfo objectForKey: @"city"], [dictionaryWeatherInfo objectForKey: @"weather1"], [dictionaryWeatherInfo objectForKey: @"temp1"]);
    15     // output
    16     NSLog(@"Weather字典里面的内容为--》%@", dictionaryWeather);
    17 }
    3.测试结果
    2013-01-27 11:14:24.649 测试[1886:c07] 今天是 2013年1月27日  星期日  北京  的天气状况是:晴  2℃~-7℃ 
    2013-01-27 11:14:24.649 测试[1886:c07] Weather字典里面的内容为--》{
        weatherinfo =     {
            city = "\U5317\U4eac";
            "city_en" = beijing;
            cityid = 101010100;
            date = "";
            "date_y" = "2013\U5e741\U670827\U65e5";
            fchh = 08;
            fl1 = "\U5c0f\U4e8e3\U7ea7";
            fl2 = "\U5c0f\U4e8e3\U7ea7";
            fl3 = "\U5c0f\U4e8e3\U7ea7";
            fl4 = "\U5c0f\U4e8e3\U7ea7";
            fl5 = "\U5c0f\U4e8e3\U7ea7\U8f6c4-5\U7ea7";
            fl6 = "\U5c0f\U4e8e3\U7ea7";
            fx1 = "\U5fae\U98ce";
            fx2 = "\U5fae\U98ce";
            img1 = 0;
            img10 = 0;
            img11 = 0;
            img12 = 99;
            img2 = 99;
            img3 = 0;
            img4 = 99;
            img5 = 0;
            img6 = 99;
            img7 = 0;
            img8 = 2;
            img9 = 2;
            "img_single" = 0;
            "img_title1" = "\U6674";
            "img_title10" = "\U6674";
            "img_title11" = "\U6674";
            "img_title12" = "\U6674";
            "img_title2" = "\U6674";
            "img_title3" = "\U6674";
            "img_title4" = "\U6674";
            "img_title5" = "\U6674";
            "img_title6" = "\U6674";
            "img_title7" = "\U6674";
            "img_title8" = "\U9634";
            "img_title9" = "\U9634";
            "img_title_single" = "\U6674";
            index = "\U51b7";
            index48 = "\U51b7";
            "index48_d" = "\U5929\U6c14\U51b7\Uff0c\U5efa\U8bae\U7740\U68c9\U8863\U3001\U76ae\U5939\U514b\U52a0\U7f8a\U6bdb\U886b\U7b49\U51ac\U5b63\U670d\U88c5\U3002\U5e74\U8001\U4f53\U5f31\U8005\U5b9c\U7740\U539a\U68c9\U8863\U6216\U51ac\U5927\U8863\U3002";
            "index48_uv" = "\U5f31";
            "index_ag" = "\U6781\U4e0d\U6613\U53d1";
            "index_cl" = "\U9002\U5b9c";
            "index_co" = "\U8f83\U4e0d\U8212\U9002";
            "index_d" = "\U5929\U6c14\U51b7\Uff0c\U5efa\U8bae\U7740\U68c9\U8863\U3001\U76ae\U5939\U514b\U52a0\U7f8a\U6bdb\U886b\U7b49\U51ac\U5b63\U670d\U88c5\U3002\U5e74\U8001\U4f53\U5f31\U8005\U5b9c\U7740\U539a\U68c9\U8863\U6216\U51ac\U5927\U8863\U3002";
            "index_ls" = "\U57fa\U672c\U9002\U5b9c";
            "index_tr" = "\U8f83\U9002\U5b9c";
            "index_uv" = "\U5f31";
            "index_xc" = "\U9002\U5b9c";
            st1 = 1;
            st2 = "-4";
            st3 = 2;
            st4 = "-3";
            st5 = 2;
            st6 = "-4";
            temp1 = "2\U2103~-7\U2103";
            temp2 = "3\U2103~-5\U2103";
            temp3 = "3\U2103~-5\U2103";
            temp4 = "3\U2103~-3\U2103";
            temp5 = "1\U2103~-7\U2103";
            temp6 = "0\U2103~-8\U2103";
            tempF1 = "35.6\U2109~19.4\U2109";
            tempF2 = "37.4\U2109~23\U2109";
            tempF3 = "37.4\U2109~23\U2109";
            tempF4 = "37.4\U2109~26.6\U2109";
            tempF5 = "33.8\U2109~19.4\U2109";
            tempF6 = "32\U2109~17.6\U2109";
            weather1 = "\U6674";
            weather2 = "\U6674";
            weather3 = "\U6674";
            weather4 = "\U6674\U8f6c\U9634";
            weather5 = "\U9634\U8f6c\U6674";
            weather6 = "\U6674";
            week = "\U661f\U671f\U65e5";
            wind1 = "\U5fae\U98ce";
            wind2 = "\U5fae\U98ce";
            wind3 = "\U5fae\U98ce";
            wind4 = "\U5fae\U98ce";
            wind5 = "\U5fae\U98ce\U8f6c\U5317\U98ce4-5\U7ea7";
            wind6 = "\U5fae\U98ce";
        };
    }
  • 相关阅读:
    自定义的事件管理器
    解决修改表结构,添加外键时出现“约束冲突”的错误
    jQuery学习(二) 自定义扩展函数
    iBt(001-004)原文与试译
    老婆怀孕了!(5+6)
    Mac_如何打开系统文件library
    Mac_如何通过命令行装包到ios手机
    MAC干净卸载pycharm
    selenium自动化_如何启动safari浏览器
    selenium自动化_click方法点击无效
  • 原文地址:https://www.cnblogs.com/ketmales/p/2878550.html
Copyright © 2020-2023  润新知