Pregunta

I have a cuestion about parsing a Json strings. If i'm getting a string like:

[["AR","Argentina","flag_of_argentina","en","F","1"],
["AU","Australia","flag_of_‌​australia","en","B","4"]]

How can i save this string into an entity when normally i see Jsons that have something like this:

(
   (
    "Group_id": 1,
    "Date" : "2014-04-08",
    "Hour" : "18:00:00",
    "Location":"Guayaquil, Ecuador",
    "State" : A
),
...

If i have to make an array or something else to store it to the entity in core data how can i do it?

I hope my question is well made ​​and I appreciate your help. Thank you very much.

¿Fue útil?

Solución

Cocoa Touch provides a built in class that helps you serialize this data. It's called NSJSONSerialization.

So, that looks to me like you have arrays within an array. First you're going to want to convert the NSString to NSData:

NSData *JSONdata = [yourJSONString dataUsingEncoding:NSUTF8StringEncoding];

Then serialize it into an array:

NSArray *JSONArray = [NSJSONSerialization JSONObjectWithData:JSONdata options:NSJSONReadingMutableContainers error:&error];

Then you can access any subarray by writing:

NSArray *countryData = [JSONArray objectAtIndex:i];

And from there you can access the data that you need.

~

As for building JSON Data, you'll need to construct an NSMutableDictionary and populate it accordingly. Once you have that, use the same class NSJSONSerialization to convert it to a JSON string.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top