首页 > 代码库 > iOS返回的不是json格式, 是NSString
iOS返回的不是json格式, 是NSString
NSDictionary *result = value[@"data"];
if (![result isKindOfClass:[NSDictionary class]]) {
NSLog(@"data 不是字典");
if ([result isKindOfClass:[NSString class]]) {
NSData *data = http://www.mamicode.com/[(NSString *)result dataUsingEncoding:NSUTF8StringEncoding];// 将数据格式转为UTF8
if (data) {
result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL];
} else {
result = @{};
}
NSLog(@"解析data: %@", result);
} else {
result = @{};
}
}
// 数组里面 放了每个字典
commentlist = "[{\"user_id\":\"1\",
\"content\":\"[\\u998b\\u5634][\\u6293\\u72c2]\",
\"createtime\":\"1970-01-01 08:00:00\",
\"realname\":\"\\u90d1\\u98de\"},
{\"user_id\":\"1\",
\"content\":\"[\\u563b\\u563b][\\u563b\\u563b][\\u563b\\u563b]\\u641e\\u5b9a\",\"createtime\":\"1970-01-01 08:00:00\",\"realname\":\"\\u90d1\\u98de\"}, {\"user_id\":\"1\",\"content\":\"\\u641e\\u5b9a\",\"createtime\":\"1970-01-01 08:00:00\",\"realname\":\"\\u90d1\\u98de\"}]";
NSDictionary *result = value[@"data"];
if (![result isKindOfClass:[NSDictionary class]]) {
NSLog(@"data 不是字典");
if ([result isKindOfClass:[NSString class]]) {
NSData *data = http://www.mamicode.com/[(NSString *)result dataUsingEncoding:NSUTF8StringEncoding];// 将数据格式转为UTF8
}}
NSData *resData = [[NSData alloc] initWithData:[@"[{\"user_id\":\"1\",\"content\":\"[\\u998b\\u5634][\\u6293\\u72c2]\",\"createtime\":\"1970-01-01 08:00:00\",\"realname\":\"\\u90d1\\u98de\"},{\"user_id\":\"1\",\"content\":\"[\\u563b\\u563b][\\u563b\\u563b][\\u563b\\u563b]\\u641e\\u5b9a\",\"createtime\":\"1970-01-01 08:00:00\",\"realname\":\"\\u90d1\\u98de\"},{\"user_id\":\"1\",\"content\":\"\\u641e\\u5b9a\",\"createtime\":\"1970-01-01 08:00:00\",\"realname\":\"\\u90d1\\u98de\"}]" dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableDictionary *detailDic;
NSArray *arr = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];
int count = arr.count;
NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count; i ++) {
detailDic = [arr objectAtIndex:i];
TestObject *testObject = [TestObject new];
testObject.user_id = [detailDic objectForKey:@"user_id"];
testObject.user_id = [detailDic objectForKey:@"content"];
testObject.user_id = [detailDic objectForKey:@"createtime"];
testObject.user_id = [detailDic objectForKey:@"realname"];
[tempArray addObject:testObject];
}
iOS返回的不是json格式, 是NSString