1

How to store this data in NSArray?

I'm getting JSON response in this format like this:

{
    1 = USA;
    4 = India;
}

I need to convert this into NSArray

1
  • 1
    How? It's an object (dictionary). You just want all values in an array? [myDictionary allValues] Commented Feb 4, 2014 at 13:58

3 Answers 3

1

If your JSON is a string, get an NSData object first:

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

Then turn it into an NSDictionary using the NSJSONSerialization class:

NSError* error = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

Since you said you want an NSArray, do the following:

NSArray *jsonArray = [jsonDict allValues];

Note that the order of the entries in the array is undefined, according to Apple's documentation. So if you need a particular order, you'll have to figure out a better approach.

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

Comments

0

This is JSON dictionary, First you convert this to NSDictionary from JSON.

NSDictionary *dictionary = //parse json using NSJSONSerilization
NSArray *array = [dictionary allValues];

allValues will return an array with all objects.

Comments

0

Try this:

NSString *jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dictionary =[jsonString JSONValue];
NSArray *array = [dictionary allValues];
NSLog(@"Array values = %@",array);

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.