-2

How to convert string to JSON object or JSON array in iOS? my JSON string Like That.

{"Data": [{"ID":"1","Name":"Raj"},{"ID":"2","Name":"Rajneesh"}]}

I want to get ID or Name from this string please help me if anyone now this.

Thank You.

I try below code but print null

  NSString *JsonString=@"{"Data": [{"ID":"1","Name":"Raj"},{"ID":"2","Name":"Rajneesh"}]}";
    NSData *objectData = [JsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData options:0 error:&error];
    NSArray* array = [json objectForKey:@"Data"];
    NSLog(@"Print Array %@",array);
Ajumal
  • 1,048
  • 11
  • 33
Rajneesh
  • 272
  • 2
  • 5
  • 16
  • [[[json valueForKey:@"Data"]objectAtIndex:indexPath.row] valueForKey:@"ID"] – Agent Chocks. Apr 01 '15 at 06:15
  • but how to convert Json String to NSDictionary? – Rajneesh Apr 01 '15 at 06:17
  • please search before ask something http://stackoverflow.com/questions/8606444/how-do-i-deserialize-a-json-string-into-an-nsdictionary-for-ios-5 – Huy Nghia Apr 01 '15 at 06:19
  • [Have you heard about `NSJSONSerialization` ?](http://stackoverflow.com/a/18036603/1603072) – Bhavin Apr 01 '15 at 06:19
  • Do a search before you post: http://stackoverflow.com/questions/8606444/how-do-i-deserialize-a-json-string-into-an-nsdictionary-for-ios-5/13411454#13411454 – Levi Apr 01 '15 at 06:20
  • You are missing some escaping characters from your string: `NSString *JsonString=@"{\"Data\": [{\"ID\":\"1\",\"Name\":\"Raj\"},{\"ID\":\"2\",\"Name\":\"Rajneesh\"}]}";` – Levi Apr 01 '15 at 06:24

3 Answers3

5

Use this

NSString *str=@"{\"Data\": [{\"ID\":\"1\",\"Name\":\"Raj\"},{\"ID\":\"2\",\"Name\":\"Rajneesh\"}]}";

NSMutableDictionary *dict=[NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];

NSMutableArray *dataArr=[dict valueForKey:@"Data"];
for (NSDictionary *userData in dataArr) {
    NSLog(@"Id:%@ Name:%@",[userData valueForKey:@"ID"],[userData valueForKey:@"Name"]);
}
Kalpesh
  • 5,336
  • 26
  • 41
1

Always Remember that when there are { } curly brackets, it means it is Dictionary and when [ ] this, means Array

NSURL *url=[NSURL URLWithString:@"Your JSON URL"];

NSData *data = [[NSData alloc] initWithContentsOfURL:url];

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

NSArray *array = json[@"Data"];

for(NSMutableDictionary *dic in array)
{
  NSLog(@"%@",dic[@"ID"]); // give 1 & 2
  NSLog(@"%@",dic[@"Name"]); // Raj and Rajneesh
}
Kumar
  • 1,882
  • 2
  • 27
  • 44
0

This is not the correct JSON string which can be parsed by Objective c, get string from encoder and you will get a valid string, other then that for JSON to Dictionary conversion is simple in iOS as its natively supported.

NSData *data = [strJSON dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data
                                                             options:kNilOptions
                                                               error:&error]; 
Retro
  • 3,985
  • 2
  • 17
  • 41