IOS system comes with JSON parsing, nsjsonreadingoptions parameter interpretation

Source: Internet
Author: User

In development, we often need to parse the JSON data, the system provides the

[nsjsonserialization jsonobjectwithdata: [OBJC datausingencoding: Nsutf8stringencoding]

Options:(nsjsonreadingallowfragments) error: &error]
Parse method, need to fill in a nsjsonreadingoptions parameter

First, use code to illustrate the role of Nsjsonreadingmutablecontainers:

    1.     NSString *str = @ "{\" name\ ": \" Alice\ "}";
    2.     
    3.     nsmutabledictionary *dict = [Nsjsonserialization jsonobjectwithdata:[str Datausingencoding:nsutf8stringencoding] options:0 Error:nil];
    4.     //program crashes, Nsjsonreadingoptions is not selected, the returned object is immutable, Nsdictionary
    5.     [dict setobject:@ "male" forkey:@ "sex"];
    6.     
    7.     nsmutabledictionary *dict = [Nsjsonserialization jsonobjectwithdata:[str Datausingencoding:nsutf8stringencoding] Options:nsjsonreadingmutablecontainers Error:nil];
    8.     //no problem, using Nsjsonreadingmutablecontainers, the returned object is mutable, Nsmutabledictionary
    9.     [dict setobject:@ "male" forkey:@ "sex"];
    10.     
    11.     nslog (@ "%@", dict);




Nsjsonreadingmutablecontainers: Returns a Variable container class, Nsmutabledictionary, or Nsmutablearray.
Nsjsonreadingmutableleaves: The value of the string in the returned JSON object is nsmutablestring, which is not currently tested on iOS 7 and should be a bug, see:
Http://stackoverflow.com/questions/19345864/nsjsonreadingmutableleaves-option-is-not-working

Nsjsonreadingallowfragments: Allows the outermost layer of the JSON string to be neither Nsarray nor nsdictionary, but it must be a valid JSON Fragment. For example, use this option to parse a string such as @ "111". See:
Http://stackoverflow.com/questions/16961025/nsjsonserialization-nsjsonreadingallowfragments-reading

IOS system comes with JSON parsing, nsjsonreadingoptions parameter interpretation

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.