我们得到了第三方API提供商之一的类似回复:
{
"$id" : "1",
"$values" : [
{
"$id" : "2",
"Description": "Description"
"$values" : [
{
"$id" : "1",
"IntProperty" : 6,
"StringProperty" : "Test 1",
"DecimalProperty" : 11.23
},
{
"$id" : "2",
"IntProperty" : 7,
"StringProperty" : "Test 2",
"DecimalProperty" : 12.23
},
{
"$id" : "3",
"IntProperty" : 8,
"StringProperty" : "Test 3",
"DecimalProperty" : 11.24
}
]
}
]
}
正如您所看到的,他们正在使用" $ id"向API用户提供db id。但是,当JSON.net尝试反序列化此类字符串时,它正在读取" $ id"作为对象参考。我尝试使用JsonProperty属性以及PreserveReferencesHandling.None和TypeNameHandling.All但这似乎不起作用。还有其他选择,而不是string.Replace(" \" $ id \""," \" id \"&# 34;?)
数据结构我尝试将其转换为:
public class RootObject
{
[JsonProperty( PropertyName = "$id" )]
public string Id { get; set; }
[JsonProperty( PropertyName = "$values" )]
public List<ChildObject> Values { get; set; }
}
public class ChildObject
{
[JsonProperty( PropertyName = "$id" )]
public string Id { get; set; }
public string Description { get; set;}
[JsonProperty( PropertyName = "$values" )]
public List<ChildObject1> Values { get; set; }
}
public class ChildObject1
{
JsonProperty( PropertyName = "$id" )]
public string Id { get; set; }
public int IntProperty { get; set; }
public string StringProperty { get; set;}
public decimal DecimalProperty { get; set;}
}