Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 句柄JSON数值变量_C#_.net_Json - Fatal编程技术网

C# 句柄JSON数值变量

C# 句柄JSON数值变量,c#,.net,json,C#,.net,Json,您好,我有这样的JSON: ratings_total: { 1: "1", 2: 0, 3: 0, 4: 0, 5: "3" } 我们如何在C#类中声明数字变量?如果您使用的是JSON.net,答案如下:Thank all的可能重复,它成功了 [DataContract] class Foo { [DataMember(Name = "ratings_total")] public Dictionary<int,int> Rating

您好,我有这样的JSON:

ratings_total: {
   1: "1",
   2: 0,
   3: 0,
   4: 0,
   5: "3"
}

我们如何在C#类中声明数字变量?

如果您使用的是JSON.net,答案如下:Thank all的可能重复,它成功了
[DataContract]
class Foo
{
    [DataMember(Name = "ratings_total")]
    public Dictionary<int,int> RatingsTotal { get; set; }
}
var json = @"{
    ""ratings_total"": {
        ""1"": ""1"",
        ""2"": 0,
        ""3"": 0,
        ""4"": 0,
        ""5"": ""3""
    }
}";

// Convert string to a stream (only nessesary for this example)
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(json);
writer.Flush();
stream.Position = 0;

// Settings to enable dictionary serialization
var settings = new DataContractJsonSerializerSettings();
settings.UseSimpleDictionaryFormat = true;
var ser = new DataContractJsonSerializer(typeof(Foo), settings);

// Deserialize object
var obj = (Foo)ser.ReadObject(stream);