我遇到了解析jSON的问题,我有一个类似的jSON:
{
"id": 0,
"message": "ok"
}
我尝试了几件事来尝试获取“id”和“message”的值,但我总是出错...
我该如何选择“id”和“message”的值?
(我在JSON
)
NSMutableArray
的结果
编辑:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSMutableDictionary *publicTimeline = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSMutableArray *sortedArray = [NSMutableArray arrayWithArray:publicTimeline.allKeys];
[sortedArray sortUsingSelector:@selector(localizedStandardCompare:)];
return sortedArray;
答案 0 :(得分:1)
试试这个,
将JSON响应解析为NSDictionary
NSDictionary * responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *message = [responseDictionary valueForKey:@"message"];
答案 1 :(得分:1)
您的JSON字符串代表字典。因此,您必须使用NSDictionary
代替NSArray
。
编辑我
// convert dictionary into JSON
NSDictionary *fromDict = @{@"id": @(0), @"message": @"ok"};
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:fromDict options:0 error:nil];
// convert data (like you get from an API request) to dictionary
NSDictionary *toDict = [NSJSONSerialization JSONObjectWithData:JSONData options:0 error:nil];
答案 2 :(得分:0)
我认为它对你有帮助(在这个回复中,你得到第一个字典和字典包含两个key.so)
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSDictionary *publicTimeline = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSString *message = [publicTimeline valueForKey:@"message"];
我觉得它对你有用。 json parser