0

I built a google speech api in c# and now google is not supporting V1 anymore so I built a code for the V2 speech api and I'm getting a different response from the server.

Here:

{"result":[]} {"result":[{"alternative":[{"transcript":"hello","confidence":0.88741958} }],"final":true}],"result_index":0}

I tried this code with JSON but it's not working:

dynamic obj = JsonConvert.DeserializeObject(responseFromServer);
            var transcript = obj.alternative[0].transcript.ToString();

It returnes an exception which says:

Additional text encountered after finishing reading JSON content: {.Path", line 2, position 1.

Thank you for helping.

user2992413
  • 77
  • 1
  • 8
  • 1
    "Not working" - what is it doing? – Rex M May 13 '14 at 13:53
  • It suppose to get the "hello" text out of the response. – user2992413 May 13 '14 at 13:55
  • That's only half the information. To get the best answers, you also need to tell us what it is currently doing! "It's not working" is not useful. Is it throwing an exception? Is it giving you the wrong text? Is it giving you nothing? – Rex M May 13 '14 at 13:57
  • Updated my post. If you need more Information just tell me. – user2992413 May 13 '14 at 14:06
  • Have you tried googling that exception message? I get some very useful explanations for the error "Additional text encountered after finishing reading JSON content". One would probably guess it means the JSON string is not in a good format. – Rex M May 13 '14 at 14:09
  • But in the web request I typed : recognize?output=json – user2992413 May 13 '14 at 14:14
  • Is there another way to do it ? – user2992413 May 13 '14 at 14:26
  • You need to surround your response with square brackets. Look at this [SO Question](http://stackoverflow.com/questions/16765877/additional-text-encountered-after-finished-reading-json-content) – Icemanind May 13 '14 at 17:13
  • @icemanind even if there were square brackets around the outer response, it still wouldn't be valid JSON. – Rex M May 15 '14 at 13:02

2 Answers2

0

The JSON payload you pasted isn't valid - which, if you search Google for the error message, the first three results address that exact issue. There are multiple root elements, closing braces in the wrong place, etc:

{
   "result":[

   ]
}
{
   "result":[
      {
         "alternative":[
            {
               "transcript":"hello",
               "confidence":0.88741958
            }
         }
      ],
      "final":true
   }
],
"result_index":0
}

I suggest doing some further research to understand why you might be getting a malformed JSON payload.

Community
  • 1
  • 1
Rex M
  • 142,167
  • 33
  • 283
  • 313
  • But this is the response I get. – user2992413 May 15 '14 at 07:06
  • So what if that's the response you get? It's still malformed JSON, which means a parser isn't going to be able to handle it, period. Now that you know it's a malformed response, you're halfway to fixing the problem. Go find out why and then you'll be even closer. – Rex M May 15 '14 at 13:04
0

Remove {"result":[]} from response string then it will become valid JSON for deserialization

Shashi Bhushan
  • 1,292
  • 11
  • 15