JSON File/URL parsing

Source: Internet
Author: User

////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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.