13

我在响应中收到以下 JSON 结果:

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

我为反序列化准备了以下类:

[DataContract]
public sealed class RpcResponse
{
    [DataMember(Name = "result")]
    public List<KeyValuePair<string, decimal>> Result { get; set; }
}

但是,当我尝试使用DataContractJsonSerializerResult属性对其进行反序列化时,最终的条目为零。Result(声明为时也不起作用Dictionary<string, decimal>

有没有办法执行此操作DataContractJsonSerializer

4

1 回答 1

0

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 }] }

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.

于 2013-02-20T13:55:18.933 回答