user384496 user384496 - 4 months ago 6
JSON Question

Pass Dictionary<String,List<String>> to java script

I would like to pass a Dictionary> to client using JavaScript.

I did look at this post and I didn't understand exactly how to proceed.
In case I'm doing something wrong I'll explain what I want to do.
The dictionary contains the 'name' key of all worksheets in the Excel file, and the 'value' is the column value of the first row in that worksheet.
The UI of the client should have two "drop list", the first will contain the key which is all the names of the worksheet in the Excel file.
The second contain all the column value of the first row of the worksheets that will choose in the first drop list – which is actually a List as the value in the dictionary.

So all the back end C# code is working fine. Now I need help in the front end JavaScript.

How do I parse the data to a key value so I can do a "search" on the keys as the client chooses some "key" in the first drop list so I can get back the relevant values as a list?

Thanx!

var ajaxRequest = $.ajax({
type: "POST",
url: "http://localhost:1894/api/Values",
contentType: false,
processData: false,
data: data,
success: function(dataTest) {

}
});


This is the JSON that I get back from the server:

{"First":["Name","Link","User","Pass"],"Sec":["test01"]}


How would I perform a search on this like in C#? I want to be able to do something like this: "dict.TryGetValue(key, out value); and the out value would return as an array of string or as a List.

Answer

Try this(you don't need var ajaxRequest variable you can directly call like this:

 $.ajax({
    type: "POST",
    url: "http://localhost:1894/api/Values",
    dataType: "json",
    data: data,
    success: function(dataTest) {
        //dataTest should contain your data in a javascript object
        for(var i = 0; i < dataTest.First.length; i++)
        {
            window.alert("" + dataTest.First[i]);
        }
        for(var i = 0; i < dataTest.Sec.length; i++)
        {
            window.alert("" + dataTest.Sec[i]);
        }
        //etc...
        //this should print the values returned if you showed me exactly how your JSON is...
    }
 });

The javascript object will contain properties with an array as the value for each property. Think of it like a map of <String, String[]>. So your returned object dataTest will have properties First and Sec and for First the value associated with the key First will be ["Name","Link","User","Pass"] which is just an array. Same for Sec. So `dataTest.First[0] will equal "Name" and dataTest.First[1] will equal "Link" etc...

*****************************************UPDATE**************************************

You can save your dataTest to a global variable in this example (myObject) then you can access like this:

var key = "First";
// Or if you want to get your key from a dropdown (select) element then you could do like this:
var key = document.getElementById("myselect").options[document.getElementById("myselect").selectedIndex].innerHTML;

if(myObject[key] != undefined)
{
    //That means there is values for this key.
    //Loop through values or do whatever you want with myObject[key].
    for(var i = 0; i < myObject[key].length; i++)
    {
        window.alert("" + myObject[key][i]);
    }
}