很简单的事情,但由于某种原因我无法弄清楚这一点。我有一个看起来像这样的模拟 JSON 文件:
{
"AccountId":"XXXXXXXXX",
"UAN":"PE3458234758345",
"BillingName":"John Smith",
"BillingAddress": {
"Address1":"701 First Ave.",
"Address2":"test",
"City":"Philadelphia",
"State":"PA",
"Zip:":"19147"
},
"ServiceStartDate":"5/1/2012",
"PromoCode":"0056",
"PartnerCode":"AAL",
"MemberNumber":"0000001",
"Invoices":[
{
"Amount":"113.78",
"Date":"6/1/2012",
"Usage":"3143"
},
{
"Amount":"123.56",
"Date":"7/1/2012",
"Usage":"4561"
},
{
"Amount":"105.23",
"Date":"8/1/2012",
"Usage":"5454"
}
],
"Expected":[
{
"AwardCreation":"true",
"AwardAmount":"500",
"AwardUnits":"usd",
"AwardDate":"today()"
}
]
}
我需要从中创建一个动态对象,因为每次运行测试模型时它都会有所不同。我已经尝试了这个解决方案中提到的自定义反序列化器以及 .NET 4.0 System.Web.Helpers,在这两种情况下我最终得到:
Invalid JSON primitive: \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...
目前我的工作测试是:
var json = new StringBuilder();
using (var fs = File.Open(@"c:\users\bryan\Desktop\test2.json", FileMode.Open))
{
var byteArray = new byte[1024];
var tempString = new UTF8Encoding(true);
while (fs.Read(byteArray, 0, byteArray.Length) > 0)
{
json.Append(tempString.GetString(byteArray));
}
}
var dynamicObject = Json.Decode(json.ToString());
Assert.IsNotNull(dynamicObject);
我不知道,我猜是格式?我已经从 json 文件中删除了所有内容(意思是一条巨大的线),我得到了同样的东西。奇怪的是,当我使用 Newtonsoft.Json 时,我没有收到错误,但动态对象只是 AccountId 字符串,没有别的。