[英]JSON has the same identifier for a property and a field, how can I get around this?
The following is a snippet of some JSON that I am receiving from a 3rd party API.以下是我从第三方 API 收到的一些 JSON 的片段。
{"for":
{
"total":
{"home":0,"away":0,"total":0},
"average":
{"home":"0.0","away":"0.0","total":"0.0"},
"total":1,
"average":"0.5"
}
}
The classes I have for deserialisation:我有反序列化的类:
public class For {
public int total { get; set; }
public string average { get; set; }
public Total total { get; set; }
public Average average { get; set; }
public Minute minute { get; set; }
public int home { get; set; }
public int away { get; set; }
}
public class Total {
public int home { get; set; }
public int away { get; set; }
public int total { get; set; }
}
public class Average {
public string home { get; set; }
public string away { get; set; }
public string total { get; set; }
}
The error:错误:
An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll Newtonsoft.Json.dll 中发生类型为“Newtonsoft.Json.JsonReaderException”的未处理异常
Additional information: Unexpected character encountered while parsing value: {.附加信息:解析值时遇到意外字符:{。 Path 'response[0].for.total', line 1, position 1047.路径 'response[0].for.total',第 1 行,position 1047。
The error when changing the case of a property and using [JsonProperty(PropertyName = "total")]
更改属性大小写并使用[JsonProperty(PropertyName = "total")]
时的错误
Additional information: A member with the name 'total' already exists on 'namespace1.For'.附加信息:名称为“total”的成员已存在于“namespace1.For”上。 Use the JsonPropertyAttribute to specify another name.使用 JsonPropertyAttribute 指定另一个名称。
Jason tolerates duplicate keys, but only the last one will be assumed. Jason 容忍重复的密钥,但只会假定最后一个。 C# doesn't allow any double properties, so the easiest way I see is to rename json keys. C# 不允许任何双重属性,所以我看到的最简单的方法是重命名 json 键。 And since nobody creates Json manually, but computer will always create the same pattern, the easiest way to do this, just to use the string functions ( or you can try regex for example).由于没有人手动创建 Json,但计算机将始终创建相同的模式,最简单的方法是使用字符串函数(或者您可以尝试使用正则表达式)。
Try this尝试这个
json=json.Replace("total\":{", "totalDetails\":{").Replace("average\":{","averageDetails\":{");
var jsonDeserialized = JsonConvert.DeserializeObject<Root>(json);
output output
{"for":{"totalDetails":{"home":0,"away":0,"total":0},"averageDetails":{"home":"0.0","away":"0.0","total":"0.0"},"total":1,"average":"0.5"}}
classes类
public class Root
{
public For @for { get; set; }
}
public class TotalDetail
{
public int home { get; set; }
public int away { get; set; }
public int total { get; set; }
}
public class AverageDetail
{
public string home { get; set; }
public string away { get; set; }
public string total { get; set; }
}
public class For
{
public TotalDetail totalDetails { get; set; }
public AverageDetail averageDetails { get; set; }
public int total { get; set; }
public string average { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.