Parse JSON in Knockout into Dictionary Key Value i

2019-09-07 08:09发布

问题:

I have following knockout text binding :

 <td><strong><span id="texthotelcode" data-bind="text: parameters"
 /></strong></td>

data binding of text: which returns data: {"id1":"2Z94","id2":"9861"}

now I want to convert them from this JSON into Key and value in Dictionary in C# as string, string

Any idea for this case thanks

回答1:

.net can deserialize JSON in the following form into c# dictionary:

dict: [ 
         { "Key": "id1", "Value": "2Z94" },
         { "Key": "id2", "Value": "9861" }
      ]

So you can use a function like this one to convert your object:

function toDictionary(data) {
    var dict = [];
    for (var prop in data)
        dict.push({ "Key": prop, "Value": data[prop] });
    return dict;
}

Then just send this object to the server.


Note that as andyp pointed out, there is a similar question with answers in this thread.

When in search for an answer, please search the site first, and post your question only when no answer fits your needs. In this case, the other thread might need some update.