2

I'm trying to parse JSON from a drupal page to an iOS device. This is the JSON format:

[{"nid":"20","name":"job 234"},{"nid":"21","name":"job 3534"}]

These are two objects. How do I save them using coredata, and a custom entity? I guess I need matching names for the attributes? Any links or tips will be helpful.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
PonyLand
  • 115
  • 2
  • 10
  • 1
    Welcome to Stack Overflow! We encourage you to [research your questions](http://stackoverflow.com/questions/how-to-ask). If you've [tried something already](http://whathaveyoutried.com/), please add it to the question - if not, research and attempt your question first, and then come back. –  Aug 29 '12 at 16:04

1 Answers1

7

PonyLand,

Your question is quite general but I'll try to give you some hints. To achieve your goal you could follow a two steps process:

  1. Parse JSON Data and retrieve objects
  2. Save objects in Core Data

To parse your JSON data you could follow working-with-json-in-ios-5. This tutorial applies to iOS 5. If iOS 5 isn't the only target you could take a look to iPhone/iOS JSON parsing tutorial resources.

Once you have parsed JSON Data, you need to use Core Data to save them. You can find how to use Core Data in the following tutorial: core-data-on-ios-5-tutorial-getting-started. Furthermore, you can find a simple explanation (made by me) on Core data in Mapping Business Objects with Core Data in iOS. If you need to use table take a look at core-data-tutorial-how-to-use-nsfetchedresultscontroller

About your Core Data model, you could create a single entity called Job (for example) with two attributes:

  • nid of type NSString (or NSNumber)
  • name of type NSString

If you want to know something else, let me know.

You could also try to use RestKit (that supports Core Data), but I think it could be easier to follow the two steps process I wrote.

Finally, Core Data programming guide is your friend.

Hope that helps.

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190