解析JSON中的字典键值在C# [英] Parse JSON in Knockout into Dictionary Key Value in C#

查看:139
本文介绍了解析JSON中的字典键值在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下敲门字文本绑定:

I have following knockout text binding :

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

文本的数据绑定:返回数据:{id1:2Z94,id2 :9861}

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

现在我想将它们从这个JSON转换为Key,并将C#中的Dictionary中的值作为字符串

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

推荐答案

.net可以将以下格式的JSON反序列化为c#字典:

.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.

请注意, andyp 指出,有一个类似的问题,答案在这个线程

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.

这篇关于解析JSON中的字典键值在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆