1. Download the JSON framework from https://github.com/stig/json-framework/: json-framework
2. Unzip the downloaded package and import all the files under the class folder into the current project.
3, add the import statement in the file used:#import "SBJson.h"
4. Convert the JSON string to a Nsdictionary object.
CPP Code
- nsstring *[email protected] "{\" China \ ": {
- \ "Beijing \": {\ "Beijing 1\": 1,\ "Beijing 2\": 2,\ "Beijing 3\": 3},
- \ "shanghai \": {\ " Shanghai 1\ ": 4,\" Shanghai 2\ ": 5,\" Shanghai 3\ ": 6},
- \ "guangzhou \": {\ "guangzhou 1\": 7,\ "Guangzhou 2\": 8,\ "Guangzhou 3\": 9}} ";
- Nsdictionary *items = [temp jsonvalue];
NSString *[email protected] "{\" China \ ": {\" beijing \ ": {\" Beijing 1\ ": 1,\" Beijing 2\ ": 2,\" Beijing 3\ ": 3}, \" Shanghai \ ": {\" Shanghai 1\ ": 4,\" Shanghai 2\ " : 5,\ "Shanghai 3\": 6}, \ "guangzhou \": {\ "guangzhou 1\": 7,\ "Guangzhou 2\": 8,\ "Guangzhou 3\": 9}} "; Nsdictionary *items = [temp Jsonvalue];
5. Recursive traversal of parsed nsdictionary objects
CPP Code
- -(void) Visitdict: (nsdictionary *) dict{
- Nsarray *keys=[dict AllKeys];
- For (NSString *key in keys) {
- NSString *result=[nsstring stringwithformat:@"key=%@,value=%@", Key,[dict Objectforkey:key]];
- NSLog (result);
- if ([[[Dict Objectforkey:key] iskindofclass:[nsdictionary class]) {
- [Self visitdict:[dict objectforkey:key]];
- }
- }
- }
-(void) Visitdict: (nsdictionary *) dict{ nsarray *keys=[dict AllKeys]; For (NSString *key in keys) { nsstring *result=[nsstring stringwithformat:@ "key=%@,value=%@", key,[dict Objectforkey:key]]; NSLog (result); if ([[[Dict Objectforkey:key] Iskindofclass:[nsdictionary class]) { [self visitdict:[dict objectforkey:key]]; } }}
6. Revert the parsed Nsdictionary object to a JSON string
CPP Code
- NSString * Jsonstr=[items jsonrepresentation];
NSString * Jsonstr=[items jsonrepresentation];
Using JSON in iOS