我无法为属性指定自定义名称。我从服务器(我无法更改)收到一些JSON,其中包含一些丑陋的属性名称。我希望C#代码坚持命名约定。
下面是我的代码(result0.StringValue保持为null):
[TestClass()]
public class WebServiceResultConverterTest
{
[DataContract(Name = "SimpleObject")]
private class SimpleObject
{
[DataMember(Name = "str_value")]
public String StringValue { get; set; }
[DataMember(Name = "int_value")]
public String IntValue { get; set; }
}
[TestMethod()]
public void DeserializeTest()
{
String input0 = @"{
""str_value"": ""This is a test string"",
""int_value"": 1664
}";
JavaScriptSerializer serializer = new JavaScriptSerializer();
SimpleObject result0 = serializer.Deserialize<SimpleObject>(input0);
Assert.AreEqual("This is a test string", result0.StringValue);
Assert.AreEqual(1664, result0.IntValue);
}
}