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

来源:互联网 发布:鲁班软件官网 编辑:程序博客网 时间:2024/06/08 13:46
////  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;}


0 0
原创粉丝点击