Search Unity

[SOLVED] JSON to Dictionary in dropdown options

Discussion in 'Scripting' started by clebertavares, Oct 23, 2019.

  1. clebertavares

    clebertavares

    Joined:
    Jan 2, 2012
    Posts:
    55
    Hello,

    I have this situation:

    public TMP_Dropdown dropdown;//assigned on Editor, cleared on start
    __

    Dictionary<string, string> myDic = new Dictionary<string, string>() //this is a example, will change...
    {
    { "Desc1", "234" },
    { "Desc2", "30" },
    { "Desc3", "50" }
    };

    dropdown.AddOptions(myDic.Keys.ToList());//this works, puts "Desc..." on dropdown options

    dropdown.onValueChanged.AddListener(DropdownValueChanged);//this works too, call DropdownValueChanged


    __

    void DropdownValueChanged(int newPosition)
    {
    string realValue = myDic.Values.ElementAt(newPosition);
    Debug.Log(realValue);
    //this works too, prints 234 to Desc1, 30 to Desc2, 50 to Desc3
    }


    __

    Now, the *problem*:

    I am downloading a JSON in that format (can not change...):

    [
    {"Id":183,"description":"blahblahblah"},
    {"Id":184,"description":"blehblehbleh"},
    {"Id":1000,"description":"and so on..."}
    ]


    How can I convert it to became a dictionary like

    {
    { "blahblahblah", "183" },
    { "blehblehbleh", "184" },
    { "and so on...", "1000" }
    }


    ?

    Thank you.
     
    Last edited: Oct 23, 2019
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    I wouldn't use a dictionary the way you are, you could end up getting the wrong element down the line. Instead, I would just pass your JSON data into an array of a class which contains the data. Populate the dropdown options with the string name from each class indexed in the array and then get the array at that index.
     
    clebertavares likes this.
  3. clebertavares

    clebertavares

    Joined:
    Jan 2, 2012
    Posts:
    55
    Hello,

    Thanks,

    But I used Newtonsoft "installing" the .DLL in Unity/Assets, and could do that:

    using Newtonsoft.Json;
    //download this https://github.com/JamesNK/Newtonsoft.Json/releases/download/12.0.2/Json120r2.zip
    //then copy the Bin\net20\Newtonsoft.Json.dll to your Assets

    So

    var results = JsonConvert.DeserializeObject<List<myData>>(myJson);
    myDic = new Dictionary<string, string>();
    foreach (var myData in results)
    {
    myDic.Add(myData.description, myData.Id);
    }
    dropdown.AddOptions(myDic.Keys.ToList());

    Just works with

    [Serializable]
    public class myData
    {
    public string Id;
    public string description;
    }

    According to:
    https://gist.github.com/onionmk2/d2e3e4cca27a37a89796e084e05de212

    "
    Why Json.Net ?
    According to https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity,

    1. Unity's JsonUtility does not support array as it is still new.
    2. Unity's JsonUtility can not serialize property.
    3. Unity's JsonUtility can not serialize Dictonary.
    so, Json.Net may be better.

    Result
    1. Json.Net does support array as it is still new.
    2. Json.Net can serialize property.
    3. Json.Net can serialize Dictonary."

      Thats it.