0

I am using Parse.com as a backend of my application, where I am currently storing the data there.

The data is a list of video game consoles. I was able to make an output of it, but instead of JSON data, the output is a big NSMutableArray.

This is my 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 output:

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";
        }
    );
}

What I want the output to be:

{
  "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"
    }
  ]
}

BY THE WAY

Why is it that when I have a separate NSData in the code:

NSData *JSONData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

And change my NSJSONSerialization to:

id listOfConsoles = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:&error];

The output will be:

The list: {
    error = unauthorized;
}
Jahm
  • 658
  • 1
  • 12
  • 27

3 Answers3

1

The first list is a NSDictionary with a single key called "results" which has an NSArray of NSDictionaries.

The second list is the same thing..

Edit 1.0:

Just to clarify, the second output is JSON, ok? Simply as that, just check here and validate yourself. The first one is the outputted JSON that is inside an NSObject. Of course there will be slight differences...

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • I see, but how come the first output is not the same as the second? How will I achieve the second output? – Jahm Mar 21 '13 at 10:50
  • You are relying on the `NSLog`, and not in what you know it will be there. I can give you an example: I know by heart, that a JSON service I have is returning strings like this "1233". The thing is that after the `NSJSONSerialization`I have `NSNumbers` instead of `NSStrings`.. So I just do `stringValue`, and that's it... – Rui Peres Mar 21 '13 at 10:52
  • As a precautionary step, I do copy the result and paste it to an online JSON validator and check if the JSON is correct or not. The 1st one is not, however the second one is. – Jahm Mar 21 '13 at 10:56
  • That's normal... It was `NSLog`ged... -.- – Rui Peres Mar 21 '13 at 12:35
  • Thanks! I just realized that;) – Jahm Mar 24 '13 at 03:32
0

Your Authentication not done successfully. Please check APPLICATION_ID and REST_API_KEY values.

Yashesh
  • 1,799
  • 1
  • 11
  • 29
  • If the authentication is not successful, why did I have the first output? – Jahm Mar 21 '13 at 10:48
  • please tell me exact step when you get response and when you get list: { error = unauthorized; } – Yashesh Mar 21 '13 at 10:53
  • I will get an `error = unauthorized` if I have a separate line for **NSData** and manually called it inside **NSJSONSerialization**. However, I can generate an output if I won't have a separate line for **NSData**. – Jahm Mar 21 '13 at 10:59
0

I was an idiot. I never realized that I tried to convert the JSON to something else, when I wanted the JSON data.

- (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);
}
Jahm
  • 658
  • 1
  • 12
  • 27