Xcode NSData到Json返回null

时间:2017-08-05 17:26:08

标签: ios json xcode

NSData *urlDatat = [NSData dataWithContentsOfURL: [NSURL URLWithString:stringURL]];

NSDictionary* json1 = [NSJSONSerialization JSONObjectWithData:urlDatat options:0 error:&error];
NSString *urlDataString =[[NSString alloc] initWithData:urlDatat encoding:NSUTF8StringEncoding];

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:urlDatat options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments|NSJSONReadingMutableLeaves  error:&error];

json1json都返回null。来自服务器的实际JSON数据为={"statusCode":"1","message":"success","utcMilliSeconds":"1501953923847"}

1 个答案:

答案 0 :(得分:0)

尝试使用

NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:urlDatat options:NSJSONReadingMutableLeaves error:&error];

<强>已更新

    NSURL *url = [NSURL URLWithString:@"https://api.kidstriangle.com/KTStandaloneAPI/Common/GetUtcMilliSeconds"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];