Nsjsonserialization can handle the Jsondata

Source: Internet
Author: User

Nsjsonserialization can handle the Jsondata

The nsjsonserialization class to convert JSON to Foundation objects and convert Foundation objects to json.

You use the Nsjsonserialization class to convert the JSON data into a foundation object or convert the foundation object into JSON data.

An object, the May is converted to JSON must has the following properties:

An object that can be converted to JSON data must have the following characteristics:

    • The top level object was an Nsarray or nsdictionary.
    • All objects is instances of nsstring, nsnumber, Nsarray, nsdictionary, or NSNull.
    • All dictionary keys is instances of nsstring.
    • Numbers is not NaN or infinity.
    • The topmost object must be Nsarray or nsdictionary
    • All instance objects must be NSString, NSNumber, Nsarray, nsdictionary, or Nsnull
    • The key values in all dictionaries must be nsstring
    • Numbers must be meaningful (cannot be infinite)

Provide the source code for the test use:

Nsdictionary+json.h and NSDICTIONARY+JSON.M

////Nsdictionary+json.h//Category////Created by youxianming on 14-8-28.//Copyright (c) 2014 youxianming. All rights reserved.//#import<Foundation/Foundation.h>@interfacensdictionary (JSON)//convert into jsonstring-(NSString *) tojsonstring;//convert into Jsondata-(NSData *) Tojsondata;@end
////nsdictionary+json.m//Category////Created by youxianming on 14-8-28.//Copyright (c) 2014 youxianming. All rights reserved.//#import "Nsdictionary+json.h"@implementationnsdictionary (JSON)-(NSString *) tojsonstring{NSData*data =[Nsjsonserialization datawithjsonobject:self OPTIONS:NSJSONWRITINGP        Rettyprinted Error:nil]; if(Data = =Nil) {        returnNil; } nsstring*string=[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; return string;}-(NSData *) tojsondata{NSData*data =[Nsjsonserialization datawithjsonobject:self OPTIONS:NSJSONWRITINGP        Rettyprinted Error:nil]; returndata;}@end

Nsdata+json.h and NSDATA+JSON.M

// //   nsdata+json.h//  Category////  Created by Youxianming on 14-8-28. //   Copyright (c) 2014 youxianming. All rights reserved. // #import <Foundation/Foundation.h>@interface  nsdata (JSON)// Convert to set -(ID) topropertylist; @end
////nsdata+json.m//Category////Created by youxianming on 14-8-28.//Copyright (c) 2014 youxianming. All rights reserved.//#import "Nsdata+json.h"@implementationNSData (JSON)- (ID) topropertylist{return[Nsjsonserialization jsonobjectwithdata:self Options:nsjsonreadingmutablele Aves Error:nil];}@end

When using the source code:

////VIEWCONTROLLER.M//JSON////Created by youxianming on 14-10-8.//Copyright (c) 2014 youxianming. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //Organizing JSON dataNsdictionary *jsondic =     @{    @"name":@"youxianming",//NSString    @" Age": @ -,//NSNumber    @"BWH": @[@1, @1, @1],//Nsarray    @"Address": @{@"Beijin":@"XXXX",@"xianning":@"YYYY"},//nsdictionary    @"Hasgirlfriend": [NSNullNULL]//NSNull    }; //convert into JsondataNSData *jsondata =[Jsondic Tojsondata]; //convert Jsondata to listNSLog (@"%@", [Jsondata topropertylist]);}@end

Here are the main points:

There is one more thing to note:

All number-related types are converted to NSNumber, either Boolean or floating-point values or integer values, and are converted to NSNumber.

Nsjsonserialization can handle the Jsondata

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.