0

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.

5
  • 1
    Is that a paste of the real JSON? The second set of JSON isn't valid at all. And it looks like a log from NSArray really... Commented Apr 9, 2014 at 21:42
  • The first is the string i get the second i writed it as example. The same string is used in android by the app of my friend and it runs good, i have some doubts about the use of it in ios. Commented Apr 9, 2014 at 21:50
  • Really sorry this is the string i get: [["AR","Argentina","flag_of_argentina","en","F","1"],["AU","Australia","flag_of_australia","en","B","4"]] Commented Apr 9, 2014 at 22:46
  • The first is real JSON, the second is an NSLog of an NSArray containing JSON data. They look similar but not identical. To convert between you use NSJSONSerialization. Commented Apr 10, 2014 at 1:02
  • possible duplicate of parse JSON string into array of objects Objective C Commented Apr 10, 2014 at 7:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.