且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 DataContractJsonSerializer 将 JSON 反序列化为字典

更新时间:2023-01-17 07:51:36

您的 JSON 字符串表示具有属性 Result 的对象,该对象本身是具有 4 个属性的对象:""、ABC、XYZ 和 Test.要使结果成为 KeyValuePairs 列表,JSON 将如下所示:

Your JSON string represents an object with a property Result that is itself an object with 4 properties: "", ABC, XYZ, and Test. For result to be a list of KeyValuePairs, the JSON would look like this:

{"result": [{ "":-41.41}, {"ABC":0.07}, {"XYZ":0.00}, {"Test":0.00 }] }

请注意,如果您在 Javascript 代码中使用关联数组,它将被序列化为具有属性值对的对象,而不是对象数组.如果这是 JSON 字符串的来源,您可能需要切换到在 JS 中使用普通索引数组来获取正确的 JSON.

Note that if you are using an associative array in Javascript code, that it is going to be serialized as an object with property-value pairs, not as an array of objects. If that is the source of the JSON string, you may need to switch to using an ordinary indexed array in your JS to get the proper JSON.