ios解析jsonファイルtouchjsonを使用して解析

1632 ワード


git hubアドレス  https://github.com/TouchCode/TouchJSON
 
 
//
//  ViewController.m
//  JsonProject
//
//  Created by seandeng on 6/1/15.
//  Copyright (c) 2015 seandeng. All rights reserved.
//

#import "ViewController.h"
#import "CJSONDeserializer.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITextView *txtView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //  API  
    NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"];
    //    NSError  ,        
    NSError *error;
    NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"jsonString--->%@",jsonString);
    //             ,     UTF8,           
    NSDictionary *rootDic = [[CJSONDeserializer deserializer] deserialize:[jsonString dataUsingEncoding:NSUTF8StringEncoding] error:&error];
    //     Json     ,            
    NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];
    NSLog(@"weatherInfo--->%@",weatherInfo);
    //    
    _txtView.text = [NSString stringWithFormat:@"    %@  %@  %@        :%@  %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end