////main.m//Oc8-json File Parsing////Created by Qianfeng on 15/6/23.//Copyright (c) 2015 Qianfeng. All rights reserved.//#import<Foundation/Foundation.h>//Json//a subset of Javascript syntax//Key:value-like key-value pairs in OC//The outermost structure of a 1.json file is usually a dictionary or an array, mostly in dictionaries.//2.json data can be basic type data, string object, Array object ([] contains), Dictionary object ({} included), NULL type, BOOL typeintMainintargcConst Char*argv[]) {@autoreleasepool {#if0//1. First read the file data into a NSData object. //nsdata *data =[nsdata datawithcontentsoffile:<# (NSString *) #>];NSString *filestring =[nsstring Stringwithcontentsoffile:@"/users/qianfeng/downloads/jsonuserlist.txt"encoding:nsutf8stringencoding Error:nil]; NSData*data =[filestring datausingencoding:nsutf8stringencoding]; //2. Parse the JSON data, parse it into a Dictionary object or an array object, see the file structureNsdictionary *DITC =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil]; NSLog (@" %@", DITC); Nsarray*users =[DITC Objectforkey:@"Users"]; for(Nsdictionary *dictinchusers) {NSLog (@"name=%@", [Dict Objectforkey:@"username"]); } #endif //Json URL ParsingNsurl*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;}
JSON File/URL parsing