0

Using the Meteor Collection API here: https://github.com/crazytoad/meteor-collectionapi

Which is crazy awesome I have been able to do CRUD operation through terminal.

When I request all the records of the collection it seems that I get back a JSON object array like this: [{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}]

Since I am interesting in using the API to perform CRUD command from my iOS app, I am wondering how would I convert from an JSON object array to initialed objects inside my iOS app?

Thanks!

Nearpoint
  • 7,202
  • 13
  • 46
  • 74

1 Answers1

1

Have you tried this?

 NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];

 NSArray* json = [NSJSONSerialization JSONObjectWithData:JSONData
                                                 options:kNilOptions
                                                   error:nil];

    if (!json) {
          NSLog(@"Error parsing JSON: %@", e);
        } else {
           for(NSDictionary *item in json) {
              NSLog(@"Item: %@", item);
           }
        }

And then loop through the array to strongly type your objects.

Or you can use one of the many open source JSON parses available: https://github.com/icanzilb/JSONModel for example.

Nearpoint
  • 7,202
  • 13
  • 46
  • 74
karlsburg
  • 185
  • 1
  • 10
  • I did but I was having trouble when trying to NSLog items from the resulting NSArray. But I understand now that it puts Dictionaries into the NSArray object. I just found this question which was exactly the same as my issue and it helped me fix it!: http://stackoverflow.com/questions/8356842/how-to-use-nsjsonserialization. – Nearpoint Sep 10 '13 at 16:30
  • I added a revision to show an example of how to loop through the data and log the items. With the revision your answer is very helpful! Thank you very much – Nearpoint Sep 10 '13 at 16:34