//
// main.m
// OC8-Json文件解析
//
// Created by qianfeng on 15/6/23.
// Copyright (c) 2015年 qianfeng. All rights reserved.
//
#import <Foundation/Foundation.h>
//Json
//Javascript 语法的子集
//key : value 类似OC中的键值对
//1.json文件 的最外层结构通常为字典或者数组,以字典居多.
//2.json数据可以为 基本类型数据,字符串对象,数组对象([]包含),字典对象({}包含),null类型,bool类型
int main(
int argc,
const char *
argv[]) {
@autoreleasepool {
#if 0
//1.先把文件的数据读成NSdata对象.
// NSData *data =[NSData dataWithContentsOfFile:<#(NSString *)#>];
NSString *fileString =[NSString stringWithContentsOfFile:
@"/Users/qianfeng/Downloads/jsonUserList.txt" encoding:NSUTF8StringEncoding error:nil];
NSData *data =
[fileString dataUsingEncoding:NSUTF8StringEncoding];
//2.解析json数据,解析成字典对象还是数组对象,看文件结构
NSDictionary *ditc =
[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@" %@",ditc);
NSArray *users =[ditc objectForKey:
@"users"];
for (NSDictionary *dict
in users) {
NSLog(@"name= %@",[dict objectForKey:
@"username"]);
}
#endif
//Json 网址解析
NSURL *url =[NSURL URLWithString:
@"http://www.oschina.net/code/snippet_160697_34201"];
NSString *urlString =
[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
//NSLog(@"%@",urlString);
NSData *data =
[urlString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *weatherInfo =
[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@" %@",weatherInfo);
}
return 0;
}
转载于:https://www.cnblogs.com/0515offer/p/4595728.html