[英]Parsing JSON Within JSON in Objective-C
I'm trying to store the JSON that is within the JSON i get from the following request... 我正在尝试存储从以下请求中获取的JSON中的JSON ...
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];
The resulting JSON obtain from body is the following and as you can see there is a JSON within the JSON, how can I store that JSON in a NSDictionary as you can see that JSON is between quotation marks. 得到的JSON从身体获得是下面的,正如你可以看到有是JSON内的JSON,我怎么可以存储 JSON在NSDictionary中 ,你可以看到,JSON是引号之间。
[
{
tag: "login",
status: true,
data:
"
{
"userID":1,
"name":"John",
"lastName":"Titor",
"username":"jtitor01",
"userEmail":"jtitor01@gmail.com",
"age":28,
}
"
}
]
What you have in reality: Classic JSON, where inside there is a String "representing" a JSON. 实际上,您拥有的是:经典JSON,其中包含一个“代表” JSON的字符串。
So, since we can do: 因此,既然我们可以做:
NSData <=> NSString NSData <=> NSString
NSArray/NSDictionary <=> JSON NSData NSArray / NSDictionary <=> JSON NSData
We just have to switch between them according to the kind of data we have. 我们只需要根据我们拥有的数据类型在它们之间切换。
NSArray *topLevelJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *lowLevelString = [[topLevelJSON firstObject] objectForKey:@"data"];
NSData *lowLevelData = [lowLevelString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *final = [NSJSONSerialization JSONObjectWithData:lowLevelData options:0 error:nil];
You should use this line of code 您应该使用此行代码
NSDictionary* responseDict = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:&errorJson];
it will help.thanks 它会有所帮助。谢谢
Try this 尝试这个
- (void)loadDetails {
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
_allVehicleLocationsArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[self afterCompleteDoThis];
}] resume];
}
- (void)afterCompleteDoThis {
for (NSDictionary *vehicleDict in _allVehicleLocationsArray) {
NSLog(@" PPP %@" , [vehicleDict valueForKey:@"vehicleType"]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.