Parse Google Translate Json C#

做~自己de王妃 提交于 2019-12-13 16:03:02

问题


I'm trying to parse some JSON using the System.Runtime.Serialization.Json library. The documentation seems a little sparse and I'm confused as to how to accomplish what I need. Here is the format for the JSON I need to parse through.

{
 "data": {
  "translations": [
   {
    "translatedText": "ne",
    "detectedSourceLanguage": "en"
   }
  ]
 }
}

回答1:


Here is a set of classes that represent the JSON data structure you have. I have chosen names that will help you correlate the type with the location in the JSON string.

[DataContract]
class RootObject
{
  [DataMember(Name = "data")]
  public DataObject Data { get; set; }
}

[DataContract]
class DataObject
{      
  [DataMember(Name="translations")]
  public List<Translation> Translations { get; set; }
}

[DataContract]
class Translation
{
  [DataMember(Name = "translatedText")]
  public string TranslatedText { get; set; }
  [DataMember(Name = "detectedSourceLanguage")]
  public string DetectedSourceLanguage { get; set; }
}

Now the following is an example of deserializing your JSON string into this structure.

  string json = @"
    {
      ""data"": {
      ""translations"": [
                          {
                            ""translatedText"": ""ne"",
                            ""detectedSourceLanguage"": ""en""
                          }
                        ]
                }
    }";

  var jsonSerializer = new DataContractJsonSerializer(typeof(RootObject));
  var o = (RootObject)jsonSerializer.ReadObject(
    new MemoryStream(Encoding.Unicode.GetBytes(json)));



回答2:


    bool Clear(string str, out string res)
    {
        var ind = str.IndexOf("[[[\"");

        if (ind == -1)
        {
            res = "";
            return false;
        }

        int end = str.IndexOf("[\"\",,,", ind + 1);

        if (end == -1)
        {
            res = "";
            return false;
        }

        res = str.Substring(ind + 2, end - ind);

        var arr = res.Split(new[] {"\",\"", "\"],[\"", "[\"", "\"]"}, StringSplitOptions.RemoveEmptyEntries);

        res = "";

        for (int i = 0; i < arr.Length; i += 2)
        {
            res += arr[i];
        }

        return true;
    }
    void TranslateText(string src_lang, string dst_lang)
    {
        var input = "Some request text";

        var url = String.Format("https://translate.google.ru/translate_a/single?client=t&sl={0}&tl={1}&hl={1}&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t&dt=at&dt=sw&ie=UTF-8&oe=UTF-8&oc=2&otf=1&trs=1&inputm=1&ssel=0&tsel=0&pc=1&q={2}", src_lang, dst_lang, input);
        var webClient = new WebClient{Encoding = Encoding.UTF8};

        webClient.DownloadStringCompleted += (sender, args) =>
        {
            string res;

            if (args.Error != null)
            {
                MessageBox.Show(args.Error.Message);

                stoped = true;

                return;
            }

            if (Clear(args.Result, out res))
            {

            }
        };

        webClient.DownloadStringAsync(new Uri(url), "your state");
    }


来源:https://stackoverflow.com/questions/7058250/parse-google-translate-json-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!