20150623_OC之Json格式的文件及URl解析

//
//  main.m
//  IOS150623_ObjectiveC_Json文件解析
//
//  Created by PengJunlong on 15/6/23.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>

//json
//javascript的子集
//key:value 類似OC中的鍵值對
//在文件中使用[]包含的是數組 ,使用{}包含的是字典對象,""包含的是字符串對象

//1.json文件最外層結構通常爲字典或者是數字,字典居多.
//2.json數據可以爲基本類型,字符串對象,數組對象([]包含),字典類型({}包含),布爾類型

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //解析json時一般只使用NSJSONSerialization中的這一個方法:+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
        //1.先把文件的數據類型讀成NSData類對象
        NSString *jsonString = [NSString stringWithContentsOfFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_ObjectiveC_Json文件解析/IOS150623_ObjectiveC_Json文件解析/jsonUserList.txt" encoding:NSUTF8StringEncoding error:nil];
        NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        
        /* Create a Foundation object from JSON data. Set the NSJSONReadingAllowFragments option if the parser should allow top-level objects that are not an NSArray or NSDictionary. Setting the NSJSONReadingMutableContainers option will make the parser generate mutable NSArrays and NSDictionaries. Setting the NSJSONReadingMutableLeaves option will make the parser generate mutable NSString objects. If an error occurs during the parse, then the error parameter will be set and the result will be nil.
         The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.
         */
        //+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
        // typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions) {
        //    NSJSONReadingMutableContainers = (1UL << 0),
        //    NSJSONReadingMutableLeaves = (1UL << 1),
        //   NSJSONReadingAllowFragments = (1UL << 2)
        // } NS_ENUM_AVAILABLE(10_7, 5_0);
        //解析json文件時options一般都選擇NSJSONReadingMutableContainers
        
        //2.解析json數據,文件最外層是字典對象
        NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"dict = %@",dict);
        NSArray *userArray = [dict objectForKey:@"users"];
        for (NSDictionary *dic in userArray) {
            NSLog(@"name = %@",[dic objectForKey:@"username"]);
        }
    }

    return 0;
}


//
//  main.m
//  IOS150623_ObjectiveC_Json網址解析
//
//  Created by PengJunlong on 15/6/23.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //解析json網址時和解析json文件一樣,只是數據的來源不同而已
        NSURL *url = [NSURL URLWithString:@"http://gc.ditu.aliyun.com/regeocoding?l=39.9333,116.3959&type=001"];
        NSString *urlString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
        NSData *data = [urlString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"dictionary = %@",dictionary);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章