locked
class for google translate api RRS feed

  • Question

  • User1069198773 posted

    Hi. I would like to get the translated (deserializing json) text using google translate api.

    {
      "data": {
        "translations": [
          {
            "translatedText": "привет"
          }
        ]
      }
    }

    This is my code, which is correct:

     protected void Page_Load(object sender, EventArgs e)
        {
                   
            string url = "https://www.googleapis.com/language/translate/v2?key=MYKEY&q=hello&source=en&target=ru";
         
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string jsonString = reader.ReadToEnd();
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            List<Translator> listtranslatedText = (List<Translator>)javaScriptSerializer.Deserialize(jsonString, typeof(List<Translator>));
           
            foreach (Translator TranslatedText in listtranslatedText)
            {
                Response.Write(TranslatedText.translatedText);
             
            }
            
    
        }
    

    The problem is with my class:

    public class Translator
    {
        public string translatedText { get; set; }
    }

    Tell me please, if this is my json: 

    { "data": { "translations": [ { "translatedText": "привет" } ] } }

    how should class Translator look like?

    Tuesday, October 6, 2020 6:18 AM

Answers

  • User753101303 posted

    Double check your RootObject class?  I made a try with a console app and your json string and it shows привет as expected:

    using System;
    using System.Web.Script.Serialization;
    
    namespace ConsoleDemo
    {
        public class Rootobject
        {
            public Data data { get; set; }
        }
        public class Data
        {
            public Translation[] translations { get; set; }
        }
        public class Translation
        {
            public string translatedText { get; set; }
        }
        public class Program
        {
            public static void Main()
            {
                Console.OutputEncoding = System.Text.Encoding.Unicode;
                string jsonString = "{\"data\":{\"translations\":[{\"translatedText\":\"привет\"}]}}";
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                Rootobject root = (Rootobject)javaScriptSerializer.Deserialize(jsonString, typeof(Rootobject));
                foreach (Translation TranslatedText in root.data.translations)
                {
                    Console.WriteLine(TranslatedText.translatedText);
                }
            }
        }
    }

    Edit you have both a RootObject and a Rootobject class maybe? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 6, 2020 11:11 AM

All replies

  • User753101303 posted

    Hi,

    { starts on object and [ an array so you have an object with a data property having a translation arrray of objects with a translatedTexct property.

    Rather than doing this by hand you can use https://www.c-sharpcorner.com/article/how-to-paste-json-as-classes-or-xml-as-classes-in-visual-stu/ whiich gives something such as :

    public class Rootobject
    {
        public Data data { get; set; }
    }
    
    public class Data
    {
        public Translation[] translations { get; set; }
    }
    
    public class Translation
    {
        public string translatedText { get; set; }
    }

    Tuesday, October 6, 2020 7:05 AM
  • User1069198773 posted

    Now I just have:

    protected void Page_Load(object sender, EventArgs e)
        {
                   
            string url = "https://www.googleapis.com/language/translate/v2?key=MYKEY&q=hello&source=en&target=ru";
         
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string jsonString = reader.ReadToEnd();
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            List<Translation> listtranslatedText = (List<Translation>)javaScriptSerializer.Deserialize(jsonString, typeof(List<Translation>));
              foreach (Translation TranslatedText in listtranslatedText)
            {
            Response.Write(TranslatedText.translatedText);
             }
          }

    and this class:

    public class Rootobject
    {
        public Data data { get; set; }
    }
    
    public class Data
    {
        public Translation[] translations { get; set; }
    }
    
    public class Translation
    {
        public string translatedText { get; set; }
    }

    But it's not working.

    Tuesday, October 6, 2020 7:13 AM
  • User753101303 posted

    What you have is a RootObject so it would be rarther something such as (untested):

    RootObject root = (RootObject)javaScriptSerializer.Deserialize(jsonString, typeof(RootObject));
    foreach (Translation t in root.data.translations) { Response.Write(t.translatedText); }
     
    Tuesday, October 6, 2020 7:50 AM
  • User1069198773 posted

    I'm getting  that RootObject does not contain data.

    Tuesday, October 6, 2020 8:49 AM
  • User753101303 posted

    Double check your RootObject class?  I made a try with a console app and your json string and it shows привет as expected:

    using System;
    using System.Web.Script.Serialization;
    
    namespace ConsoleDemo
    {
        public class Rootobject
        {
            public Data data { get; set; }
        }
        public class Data
        {
            public Translation[] translations { get; set; }
        }
        public class Translation
        {
            public string translatedText { get; set; }
        }
        public class Program
        {
            public static void Main()
            {
                Console.OutputEncoding = System.Text.Encoding.Unicode;
                string jsonString = "{\"data\":{\"translations\":[{\"translatedText\":\"привет\"}]}}";
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                Rootobject root = (Rootobject)javaScriptSerializer.Deserialize(jsonString, typeof(Rootobject));
                foreach (Translation TranslatedText in root.data.translations)
                {
                    Console.WriteLine(TranslatedText.translatedText);
                }
            }
        }
    }

    Edit you have both a RootObject and a Rootobject class maybe? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 6, 2020 11:11 AM
  • User1069198773 posted

    thank you very much

    Tuesday, October 6, 2020 11:31 AM