user3271123
user3271123

Reputation: 37

Json values into NSArray Parsing in IOS

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

Upvotes: 1

Views: 209

Answers (3)

Gavin
Gavin

Reputation: 8200

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.

Upvotes: 1

Rajesh Loganathan
Rajesh Loganathan

Reputation: 11217

Try this:

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

Upvotes: 0

Adnan Aftab
Adnan Aftab

Reputation: 14477

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.

Upvotes: 0

Related Questions