【问题标题】:Parse Google Speech Kit JSON in iOS在 iOS 中解析 Google Speech Kit JSON
【发布时间】:2015-08-03 09:58:23
【问题描述】:

我收到了来自 Google 语音 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
}

但是我在解析 JSON 响应时遇到了困难。我有以下代码

第一种方法:我尝试打印时得到一个空结果

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

第二种方法:当我尝试打印时得到相同的空结果

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

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

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

此外,当我尝试通过 http://jsonlint.com/ 验证 JSON 响应时,我收到以下消息

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

【问题讨论】:

    标签: ios objective-c iphone json google-speech-api


    【解决方案1】:

    您的回复不是正确的 json 格式。首先添加以下行以通过以下行删除多余的空结果字符串:

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

    然后,试试下面的代码:

        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");
        }
    

    【讨论】:

      猜你喜欢
      • 2014-11-10
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多