SO I am getting a response from the google translate API’s like this:
"{\"responseData\": {\"translatedText\":\"Hola Mi nombre es\"}, \"responseDetails\": null, \"responseStatus\": 200}"
My code looks like:
private string getText(string response)
{
var serializer = new JavaScriptSerializer(new SimpleTypeResolver());
translationReturnObject obj = serializer.Deserialize<translationReturnObject>(response);
return obj.translatedText;
}
with translationReturnObject defined like this:
[DataContract]
class translationReturnObject
{
[DataMember(Name = "responseData")]
public string repsonseData { get; set;}
[DataMember(Name = "translatedText")]
public string translatedText { get; set; }
[DataMember(Name = "responseDetails")]
public string responseDetails { get; set; }
[DataMember(Name = "responseStatus")]
public string responseStatus { get; set; }
}
My problem is that translatedText is not getting deserialized correctly. No matter what the language being returned is (so I don’t think it is an Encoding issue) the value is null.
Any ideas on what I am doing wrong?
If you look at the JSON that’s returning, you’ll see that
translatedTextis a property ofresponseData. Therefore, you’ll need the following:EDIT: Also, you don’t need to worry about
DataContract/DataMember.JavaScriptSerializerwill map the JSON name to the public property name.