zoukankan      html  css  js  c++  java
  • iOS原生JSON解析.

    - (IBAction)accessInterfaceBtnPressed:(id)sender {
        
        NSError *error;
        NSString *URL=@"https://api.thinkpage.cn/v3/life/suggestion.json?key=aacuhmmvipa30v3f&location=shanghai&language=zh-Hans";
        //加载一个NSURL对象
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
        //将请求的url数据放到NSData对象中
        NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
        NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
       // NSLog(@"All: %@", weatherDic );
        NSDictionary *weatherInfo = [weatherDic objectForKey:@"results"];
        
        NSArray * resultArray=[weatherDic objectForKey:@"results"];
        // NSLog(@"dictionary length is: %d",[weatherInfo count]);
        // NSLog(@"weatherInfo: %@", weatherInfo );
        // NSString *updateTime=[weatherInfo objectForKey:@"last_update"];
        NSLog(@"updateTime: %@", [weatherInfo valueForKey:@"last_update"] );
        //[resultArray valueForKey:@"location"]valueForKey:@"name" ]

        NSArray *name=[[resultArray valueForKey:@"location"]valueForKey:@"name"];
        NSString *shanghai=name[0];
        
        //NSString *stringData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
       // NSString *resultStr=[[NSString alloc]initWithUTF8String:@"aaa"];
        NSLog(@"shanghai: %@", shanghai);
        
        
        NSDictionary *locationInfo = [weatherInfo valueForKey:@"location"];
        
       
        //NSLog(@"last_update: %@", [weatherInfo objectForKey:@"last_update"] );
        // NSMutableString *name=[locationInfo valueForKey:@"name"];
        // NSLog(@"name: %@", name);
       // NSString*jsonString = [[NSString alloc]initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding] ;
        // NSData *data=[locationInfo valueForKey:@"name"];
        [self textView].text = shanghai;
    }

    参考:

    https://temboo.com/ios/parsing-json

     How it appears in the JSON string
    NSArray "name": [
    NSObject "name": {

    另外, 原来()是特么一个数组的意思...搞得我各种找编码的方法...嫩娘...怪我罗....

  • 相关阅读:
    搭建docker registry私有镜像仓库
    安装go client调用Kubernetes API
    Kubernetes搭建Hadoop服务
    Kubernetes部署Kafka集群
    Kubernetes使用StorageClass动态生成NFS类型的PV
    Linux配置网络服务
    Kubernetes设置命名空间
    Kubernetes的简单Demo
    docker操作的一些例子
    MySQL的一些基本概念
  • 原文地址:https://www.cnblogs.com/Montauk/p/5761171.html
Copyright © 2011-2022 走看看