//解析数据 SBJsonParser *parser = [[SBJsonParser alloc] init]; NSString *jsonStr = <p class="p1">@"{\"customer\":[{\"name\":\"roamer\",\"ycount\":\"232.4\",\"sumcount\":\"322.3\"},{\"name\":\"<span class="s1">王三</span>\",\"ycount\":\"221.2\",\"sumcount\":\"1123.2\"},{\"name\":\"<span class="s1">李四</span>\",\"ycount\":\"1221.2\",\"sumcount\":\"12123.2\"}]}"<span class="s2">;</span></p> NSError *error = nil; NSMutableDictionary *root = [[NSMutableDictionary alloc] initWithDictionary:[parser objectWithString:jsonStr error:&error]]; //转换代码 不然会带来中文问题,诸如加()、:变成 = 等乱码问题 SBJsonWriter *jsonWrite = [[SBJsonWriter alloc] init]; NSString *jsonString = [jsonWrite stringWithObject:root]; NSLog(@"JsonString:%@", jsonString); [jsonWrite release]; NSMutableArray *customers = [root objectForKey:@"customer"]; NSLog(@"Customers:%@", customers); for (NSMutableDictionary *customer in customers) { NSLog(@"Customer-name:%@", [customer objectForKey:@"name"]); } [self.jsonData setText:@"解析完成"]; [self.parseBtn setTitle:@"Done" forState:UIControlStateNormal];
使用TouchJson和SBJson都可以快速的完成Json数据的解析,两者使用起来都比较简单,但TouchJson性能更好一些推荐使用。