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:
- NSString *str = @ "{\" name\ ": \" Alice\ "}";
-
- nsmutabledictionary *dict = [Nsjsonserialization jsonobjectwithdata:[str Datausingencoding:nsutf8stringencoding] options:0 Error:nil];
- //program crashes, Nsjsonreadingoptions is not selected, the returned object is immutable, Nsdictionary
- [dict setobject:@ "male" forkey:@ "sex"];
-
- nsmutabledictionary *dict = [Nsjsonserialization jsonobjectwithdata:[str Datausingencoding:nsutf8stringencoding] Options:nsjsonreadingmutablecontainers Error:nil];
- //no problem, using Nsjsonreadingmutablecontainers, the returned object is mutable, Nsmutabledictionary
- [dict setobject:@ "male" forkey:@ "sex"];
-
- 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