lundi 3 août 2015

Parse Google Speech Kit JSON in iOS

I am having the following JSON response from Google speech API

    {
    "result": [

    ]
}{
    "result": [
        {
            "alternative": [
                {
                    "transcript": "testing 123"
                },
                {
                    "transcript": "listing 123"
                },
                {
                    "transcript": "casting 123"
                },
                {
                    "transcript": "fasting 123"
                },
                {
                    "transcript": "listing 1 2 3"
                },
                {
                    "transcript": "Justin 123"
                },
                {
                    "transcript": "listening 123"
                },
                {
                    "transcript": "listen 123"
                }
            ],
            "final": true
        }
    ],
    "result_index": 0
}

However I am having difficulties in parsing the JSON response. I have the following code

First approach: I get an empty result when I try to print

NSDictionary *results = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];
NSDictionary *resultsDictionary = [[results objectForKey:@"result"] objectAtIndex:0];
    NSLog(@"result %@", resultsDictionary);

Second approach: getting the same empty result when I try to print

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

NSArray* ResultArray = [json objectForKey:@"result"];

NSLog(@"result: %@", ResultArray);

Aucun commentaire:

Enregistrer un commentaire