標籤:style blog http io ar os 使用 java sp
1、從https://github.com/stig/json-framework/中下載json架構:json-framework
2、解壓下載的包,將class檔案夾下的所有檔案匯入到當前工程下。
3、在使用的檔案中加入匯入語句 :#import "SBJson.h"
4、將json字串轉為NSDictionary對象。
Cpp代碼
- NSString *[email protected]"{\"中國\":{
- \"北京\":{\"北京1\":1,\"北京2\":2,\"北京3\":3},
- \"上海\":{\"上海1\":4,\"上海2\":5,\"上海3\":6},
- \"廣州\":{\"廣州1\":7,\"廣州2\":8,\"廣州3\":9}}}";
- NSDictionary *items = [temp JSONValue];
NSString *[email protected]"{\"中國\":{ \"北京\":{\"北京1\":1,\"北京2\":2,\"北京3\":3}, \"上海\":{\"上海1\":4,\"上海2\":5,\"上海3\":6}, \"廣州\":{\"廣州1\":7,\"廣州2\":8,\"廣州3\":9}}}"; NSDictionary *items = [temp JSONValue];
5、遞迴遍曆解析出的NSDictionary對象
Cpp代碼
- -(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、將解析出的NSDictionary對象還原為json字串
Cpp代碼
- NSString * jsonStr=[items JSONRepresentation];
NSString * jsonStr=[items JSONRepresentation];
在IOS中使用json