简体   繁体   English

在iOS中解析Google Speech Kit JSON

[英]Parse Google Speech Kit JSON in iOS

I am having the following JSON response from Google speech API 我收到来自Google Speech API的以下JSON响应

    {
    "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. 但是我在解析JSON响应时遇到困难。 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);

Also when I try to validate the JSON response through http://jsonlint.com/ , I am getting the following message 另外,当我尝试通过http://jsonlint.com/验证JSON响应时,我收到以下消息

Parse error on line 5:
...: [            ]}{    "result": [  
--------------------^
Expecting 'EOF', '}', ',', ']'

Your response is not in proper json format. 您的回复格式不正确。 First add the below line to remove the extra empty result string by following line: 首先添加以下行以通过以下行删除多余的空结果字符串:

yourJsonString = [yourJsonString stringByReplacingOccurrencesOfString:@"{\"result\":[]}" withString:@""];

Then, Try out the below code: 然后,尝试以下代码:

    yourJsonString = [yourJsonString stringByReplacingOccurrencesOfString:@"{\"result\":[]}" withString:@""];

    NSData* jsonData = [yourJsonString dataUsingEncoding:NSUTF8StringEncoding];

    NSError *error = nil;
    NSDictionary *responseObj = [NSJSONSerialization
                                 JSONObjectWithData:jsonData
                                 options:0
                                 error:&error];

    if(! error) {
        NSArray *responseArray = [responseObj objectForKey:@"result"];
        for (NSDictionary *alternative in responseArray) {
            NSArray *altArray = [alternative objectForKey:@"alternative"];
            for (NSDictionary *transcript in altArray) {
                NSLog(@"transcript : %@",[transcript objectForKey:@"transcript"]);
            }
        }

    } else {
        NSLog(@"Error in parsing JSON");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM