Use json in IOS and json in IOS
1. Download jsonframework :json-framework from https://github.com/stig/json-framework /.
2. decompress the downloaded package and import all files in the class folder to the current project.
3. Add the import Statement to the file used: # import "SBJson. h"
4. Convert the json string into an NSDictionary object.
Cpp Code
NSString * temp = @ "{\" 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 * temp = @ "{\" 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. recursively traverse the parsed NSDictionary object
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. Restore the parsed NSDictionary object to a json string
Cpp Code
NSString * jsonStr = [items JSONRepresentation];
NSString * jsonStr=[items JSONRepresentation];
IOS development, how to use json files
JSON is generally a piece of data requested from the network interface. first, you need to send a request to the server, get a piece of JSON, and then parse it. two third-party open source class libraries, ASIHTTPRequest and SBJSON, are used.
NSURL * url = [NSURL URLWithString: [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
SendRequest = [ASIHTTPRequest requestWithURL: url];
[SendRequest setTimeOutSeconds: 30];
[SendRequest setDelegate: self];
[SendRequest startAsynchronous];
-(Void) requestFinished :( ASIHTTPRequest *) request
{
NSString * responseString = [request responseString];
If (responseString = nil | [responseString JSONValue] = nil ){
Return;
}
NSDictionary * responseDict = [responseString JSONValue];
Int result = [[responseDict objectForKey: @ "status"] intValue];
If (result = 1 ){
NSArray * location = [responseDict objectForKey: @ "locations"];
...............................
}
How to get a json package in the ios path
NSString * strPath = [[NSBundle mainBundle] pathForResource: @ "json file name" ofType: @ "json"]; NSString * str = [NSString stringWithContentsOfFile: strPath encoding: NSUTF8StringEncoding error: nil];