简体   繁体   中英

Parse Google speech kit response in ios

I am using Google api in an iOS app. 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 . But I need the valid result in NSArray of NSDictionary .

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

This gives me the string result but how to convert it to 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"];
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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