我想存儲的JSON是JSON我從下面的請求得到內...解析JSON在JSON在Objective-C
NSURL *URL = [NSURL URLWithString:@"http://www.demo.com/server/rest/login?username=admin&password=123"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"GET"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
// Handle error...
return;
}
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSLog(@"Response HTTP Status code: %ld\n", (long)[(NSHTTPURLResponse *)response statusCode]);
NSLog(@"Response HTTP Headers:\n%@\n", [(NSHTTPURLResponse *)response allHeaderFields]);
}
NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Response Body:\n%@\n", body);
}];
[task resume];
產生的JSON從體獲得的是以下內容以及您可以看到JSON中存在JSON,如何將 JSON存儲在NSDictionary中,因爲您可以看到JSON位於引號之間。
[
{
tag: "login",
status: true,
data:
"
{
"userID":1,
"name":"John",
"lastName":"Titor",
"username":"jtitor01",
"userEmail":"[email protected]",
"age":28,
}
"
}
]
'的NSArray *頂層= [NSJSONSerialization JSONObjectWithData:數據選項:0錯誤:無]; NSString * lowLevel = [[topLevel firstObject] objectForKey:@「data」]; NSData * lowLevelData = [lowLevel dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary * final = [NSJSONSerialization JSONObjectWithData:lowLevelData options:0 error:nil];'或類似的東西可以做到這一點。 – Larme
是的,它的工作,謝謝! – xzrudy