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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值