星五博客

iOS5自带json解析体验

//JSON内容:{"Balance":0,"result":"success","ContentLenght":[240,67,60],"User":"测试"}
//IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSData *mData = [@"{\"Balance\":0,\"result\":\"success\",\"ContentLenght\":[240,67,60],\"User\":\"测试\"}" dataUsingEncoding:NSUTF8StringEncoding];
NSError *err = nil;
NSDictionary *mDic = [NSJSONSerializationJSONObjectWithData:mData options:NSJSONReadingMutableLeaveserror:&error];
if (err) {
  NSLog(@"JSON解析错误");
}else{
  NSLog(@"字典内容:%@", mDic);
  NSLog(@"余额:%@", [mDic objectForKey:@"Balance"]);
  NSArray *contentLenght=[mDic objectForKey:@"ContentLenght"];
  NSLog(@"内容长度限制(上限):%@", contentLenght[0]);
  NSLog(@"内容长度限制(单条):%@", contentLenght[1]);
  NSLog(@"内容长度限制(多条):%@", contentLenght[2]);
  NSLog(@"用户:%@", [mDic objectForKey:@"User"]);
  NSLog(@"结果:%@", [mDic objectForKey:@"result"]);
}

输出结果

字典内容:{
    Balance = 0;
    ContentLenght =     (
        240,
        67,
        60
    );
    User = "\U6d4b\U8bd5";
    result = success;
}
余额:0
内容长度限制(上限):240
内容长度限制(单条):67
内容长度限制(多条):60
用户:测试
结果:success

IOS