我在iOS应用中使用Google API。答案是这样的:
{
"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
}
我正在获取数据但是如何解析此响应?
我把它转换为NSString
。但我需要NSArray
的{{1}}中的有效结果。
NSDictionary
这为我提供了字符串结果,但如何将其转换为NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
?
答案 0 :(得分:0)
@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"];
}
}