-3

I want to parse a plain JSON array like:

{
    "ns": [
        [
            "1364987475027",
            "Alert1",
            "001"
        ],
        [
            "1364987475042",
            "Alert2",
            "001"
        ],
        [
            "1364987475058",
            "Alert4",
            "001"
        ]
    ]
}

To get the array in simple arrays of string. I found many posts with JSON dictionary arrays. But in this case the JSON is not having keys for the values. Kindly help.

Sangram Mohite
  • 735
  • 4
  • 11
  • 23

4 Answers4

17

Answer : NSJSONSerialization.

NSJSONSerialization class can be used to convert JSON to Foundation objects and Foundation objects to JSON. In your case, you should use -JSONObjectWithData:options:error: of NSJSONSerialization class to retrieve a Foundation object from given JSON data.


Sample Code :

NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray *fetchedArr = [json objectForKey:@"ns"];
Bhavin
  • 27,155
  • 11
  • 55
  • 94
2

The NSJSONSerialization method JSONObjectWithData:options:error can to that. You will get a dictionary with one value for the key "ns" and the value will be an array of arrays.

dasdom
  • 13,975
  • 2
  • 47
  • 58
2

And the description shows what you are looking for

   NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSArray *fetchedArr = [json objectForKey:@"ns"];

    for (NSArray *arr in fetchedArr) {
        [arr description];
    }
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
2
 NSError *error = NULL;
NSData* data = [yourJsonString dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary* json = [NSJSONSerialization
                                  JSONObjectWithData:data
                                  options:kNilOptions
                                  error:&error];
NSArray *resultArray = [json objectForKey:@"ns"];//resultArray contains array type objects...

I think it will be helpful to you.

Prasad G
  • 6,702
  • 7
  • 42
  • 65