none
How to deserialize a JSON string whose elements aren't known ahead of time RRS feed

  • Question

  • Hi there,

    Given the following JSON string (a subset of a much larger string), which is being returned by a MSFT web service (so I don't control the format):

    {

       "translation": {
            "af": {
                "name": "Afrikaans",
                "nativeName": "Afrikaans",
                "dir": "ltr"
            },
            "ar": {
                "name": "Arabic",
                "nativeName": "العربية",
                "dir": "rtl"
            }
       }
    }


    How do I deserialize this in C# (what does the corresponding C# class look like), or how do I manually iterate each element so I can populate a C# class (without using "Json.NET" - I'm restricted to native .NET V3.5 or earlier classes only, not V4.X classes). In particular, note the occurrence of "af" and "ar". These are language codes that differ for every language, so I don't know their names ahead of time (in order to construct a class with those names). According to the official (MSFT) web service docs I'm calling to retrieve this string (see Translator Text API 3.0: Languages), these codes are actually dictionary keys:

    The value of the translation property is a dictionary of (key, value) pairs. Each key is a BCP 47 language tag. A key identifies a language for which text can be translated to or translated from. The value associated with the key is a JSON object with properties that describe the language ...

    Any assistance is greatly appreciated (note that I'm new to JSON). Thanks.


    Larry (25+ years on MSFT platforms, mostly in C/C++)


    Saturday, January 26, 2019 1:17 PM

Answers

  • If you cannot yet create a class for such data, then try deserialising into a dictionary:

     

    string json = . . .

     

    var s = new System.Web.Script.Serialization.JavaScriptSerializer();

    var d = s.Deserialize<Dictionary<string, object>>( json );

     

    // example: get native name of "ar"

    var t = (IDictionary<string, object>)d["translation"];

    var ar = (IDictionary<string, object>)t["ar"];

    string native_name = (string)ar["nativeName"];

     

    Also add a reference to System.Web.Extensions.


    Saturday, January 26, 2019 6:03 PM

All replies

  • If you cannot yet create a class for such data, then try deserialising into a dictionary:

     

    string json = . . .

     

    var s = new System.Web.Script.Serialization.JavaScriptSerializer();

    var d = s.Deserialize<Dictionary<string, object>>( json );

     

    // example: get native name of "ar"

    var t = (IDictionary<string, object>)d["translation"];

    var ar = (IDictionary<string, object>)t["ar"];

    string native_name = (string)ar["nativeName"];

     

    Also add a reference to System.Web.Extensions.


    Saturday, January 26, 2019 6:03 PM
  • Thanks very much. I'll review it further but there's already an issue. I can't hardcode "ar" because I don't know that particular element ahead of time (nor any of the others at the same level - only its 3 children's names are known ahead of time). This is the problem I'm having.

    Larry (25+ years on MSFT platforms, mostly in C/C++)

    Saturday, January 26, 2019 6:29 PM
  • Sorry, I obviously responded too soon (mea culpa). I just reviewed it and it does solve the problem (though why MSFT chose such a cumbersome design is anyone's guess - doesn't lend itself to a clean deserialization IMHO). In any case, thanks very much (greatly appreciated!).

    Larry (25+ years on MSFT platforms, mostly in C/C++)


    Saturday, January 26, 2019 8:09 PM