简体   繁体   English

在iOS中解析Google语音工具包响应

[英]Parse Google speech kit response in ios

I am using Google api in an iOS app. 我在iOS应用中使用Google api。 The response is like this: 响应是这样的:

{
    "result":[]
}

{  
   "result":[  
      {  
         "alternative":[  
            {  
               "transcript":"hello hello",
               "confidence":0.94471323
            },
            {  
               "transcript":"Hello-Hello"
            },
            {  
               "transcript":"hello jello"
            },
            {  
               "transcript":"hello hello hello"
            },
            {  
               "transcript":"hello hello."
            }
         ],
         "final":true
      }
   ],
   "result_index":0
}

I am getting the data but how to parse this response? 我正在获取数据,但是如何解析此响应?

I converted it to a NSString . 我将其转换为NSString But I need the valid result in NSArray of NSDictionary . 但是我需要NSDictionary NSArray中的有效结果。

NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

This gives me the string result but how to convert it to NSDictionary ? 这给了我字符串结果,但是如何将其转换为NSDictionary

    @try
    {
        NSString *finalSpeech;
        NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"responseString: %@",responseString) ;

        NSRange range = [responseString rangeOfString:@"\n"];
        if (range.location != NSNotFound)
        {
            NSString *resultString = [responseString substringFromIndex:range.location];
            NSLog(@"%@",resultString);
            NSData *jsonData = [resultString dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
//            NSLog(@"dic: %@",dic) ;
            NSArray *arrResult = [dic objectForKey:@"result"];
            NSDictionary *speech = [[[arrResult objectAtIndex:0] valueForKey:@"alternative"]objectAtIndex:0];
//            NSLog(@"speech is :%@",speech);
            finalSpeech = [speech objectForKey:@"transcript"];
        }

    }

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

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