我正在使用Parse.com作为我的应用程序的后端,我目前正在那里存储数据。
数据是视频游戏控制台的列表。我能够输出它,但输出是一个很大的NSMutableArray而不是JSON数据。
这是我的viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"https://api.parse.com/1/classes/Consoles"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"APPLICATION_ID" forHTTPHeaderField:@"X-Parse-Application-Id"];
[request setValue:@"REST_API_KEY" forHTTPHeaderField:@"X-Parse-REST-API-Key"];
NSError *error;
id listOfConsoles = [NSJSONSerialization JSONObjectWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil] options:NSJSONReadingMutableContainers error:&error];
NSLog(@"The list: %@", listOfConsoles);
}
输出:
The list: {
results = (
{
createdAt = "2013-03-21T07:26:04.149Z";
name = PlayStation;
objectId = vloIK0MZIA;
updatedAt = "2013-03-21T07:26:04.149Z";
},
{
createdAt = "2013-03-21T07:26:34.209Z";
name = Wii;
objectId = RIRpgbznlq;
updatedAt = "2013-03-21T07:26:34.209Z";
},
{
createdAt = "2013-03-21T07:26:39.391Z";
name = Xbox;
objectId = xBNgHtJbrV;
updatedAt = "2013-03-21T07:26:39.391Z";
}
);
}
我想要的输出是什么:
{
"results" : [
{
"objectId" : "vloIK0MZIA",
"updatedAt" : "2013-03-21T07:26:04.149Z",
"createdAt" : "2013-03-21T07:26:04.149Z",
"name" : "PlayStation"
},
{
"objectId" : "RIRpgbznlq",
"updatedAt" : "2013-03-21T07:26:34.209Z",
"createdAt" : "2013-03-21T07:26:34.209Z",
"name" : "Wii"
},
{
"objectId" : "xBNgHtJbrV",
"updatedAt" : "2013-03-21T07:26:39.391Z",
"createdAt" : "2013-03-21T07:26:39.391Z",
"name" : "Xbox"
}
]
}
按方式
为什么我在代码中有一个单独的 NSData :
NSData *JSONData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
将我的 NSJSONSerialization 更改为:
id listOfConsoles = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:&error];
输出将是:
The list: {
error = unauthorized;
}
答案 0 :(得分:1)
第一个列表是NSDictionary
,其中包含一个名为“results”的密钥,其NSArray
为NSDictionaries
。
第二个列表是一样的..
修改1.0:
只是为了澄清,第二个输出是JSON,好吗?就这样,只需检查here并验证自己。第一个是NSObject
内输出的JSON。当然会有轻微的差异......
答案 1 :(得分:0)
您的身份验证未成功完成。请检查 APPLICATION_ID 和 REST_API_KEY 值。
答案 2 :(得分:0)
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"https://api.parse.com/1/classes/Consoles"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"APPLICATION_ID" forHTTPHeaderField:@"X-Parse-Application-Id"];
[request setValue:@"REST_API_KEY" forHTTPHeaderField:@"X-Parse-REST-API-Key"];
NSString *listOfConsoles = [[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil] encoding:NSUTF8StringEncoding];
NSLog(@"%@", listOfConsoles);
}