0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Rashad
  • 11,057
  • 4
  • 45
  • 73

1 Answers1

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

    }