C# 句柄JSON数值变量
您好,我有这样的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
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);