开发者

Parse Google Translate Json C#

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


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)));


    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");
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜