我无法为属性指定自定义名称。我从服务器收到了一些带有一些丑陋属性名称的 JSON(我无法更改)。我希望 C# 代码遵守命名约定。
下面是我的代码(result0.StringValue 保持为空):
[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);
}
}