How to deserialize google translate API result by not using dynamic?

I tried to translate text by using the method of the link below

the main part of the code:

public string TranslateText(string input)
    {
      // Set the language from/to in the url (or pass it into this function)
      string url = String.Format
      ("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
       "en", "es", Uri.EscapeUriString(input));
      HttpClient httpClient = new HttpClient();
      string result = httpClient.GetStringAsync(url).Result;

      // Get all json data
      var jsonData = new JavaScriptSerializer().Deserialize<List<dynamic>>(result);

      // Extract just the first array element (This is the only data we are interested in)
      var translationItems = jsonData[0];

      // Translation Data
      string translation = "";

      // Loop through the collection extracting the translated objects
      foreach (object item in translationItems)
      {
        // Convert the item array to IEnumerable
        IEnumerable translationLineObject = item as IEnumerable;

        // Convert the IEnumerable translationLineObject to a IEnumerator
        IEnumerator translationLineString = translationLineObject.GetEnumerator();

        // Get first object in IEnumerator
        translationLineString.MoveNext();

        // Save its value (translated text)
        translation += string.Format(" {0}", Convert.ToString(translationLineString.Current));
      }

      // Remove first blank character
      if (translation.Length > 1) { translation = translation.Substring(1); };

      // Return translation
      return translation;
    }

This works on editor and Mono build. but the “dynamic” will crash il2cpp build, becuase it is not surported.

so how can I deserialize this result.
the file attached is the result recieved.

8449160–1120694–translate.7z (5.31 KB)

Wild guess, just use lower case object instead?

1 Like

can’t do. there is a foreach below

seems like it work, I add a force convert

test ok. problem solved. Thanks very much for answering.
you also helped my question last time, I remember.

        var jsonData = Json.ToObject<List<object>>(result);

        //string translation = (((string)jsonArray[0]));

        // Extract just the first array element (This is the only data we are interested in)
        var translationItems =  (IEnumerable)( jsonData[0] );
1 Like