문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top