C# 如何反序列化json响应
我将收到如上所示的json响应。项目的数量可能会增加,例如现在有2个,它可能会根据数据库中记录的数量而增加。另一点是,上面显示的每个属性的值始终与上面的格式类似 我的问题是,当我使用下面的类对json响应进行反序列化时,它不起作用:C# 如何反序列化json响应,c#,json,json.net,json-deserialization,C#,Json,Json.net,Json Deserialization,我将收到如上所示的json响应。项目的数量可能会增加,例如现在有2个,它可能会根据数据库中记录的数量而增加。另一点是,上面显示的每个属性的值始终与上面的格式类似 我的问题是,当我使用下面的类对json响应进行反序列化时,它不起作用: [ { "property1": "prop-00000001", "property2": "property2value1", "property3": {}, "property4": [
[
{
"property1": "prop-00000001",
"property2": "property2value1",
"property3": {},
"property4": [
"Prop4-00000001",
"Prop4-00000002"
]
},
{
"property1": "prop-00000002",
"property2": "property2value2",
"property3": {},
"property4": [
"Prop4-00000003",
"Prop4-00000004"
]
}
]
如何编写正确的C#类或C#解决方案
公共类EmptyClass
{
//根据您的响应将属性添加到此类。
}
公共类1
{
//由于响应json具有camelCasing,因此需要定义JsonProperty来表示camelCasing,或者只使用公共字符串property1{get;set;}而不进行任何修饰。
[JsonProperty(“property1”)]
公共字符串属性1{get;set;}
[JsonProperty(“property2”)]
公共字符串属性2{get;set;}
[JsonProperty(“property3”)]
公共EmptyClass属性3{get;set;}
[JsonProperty(“property4”)]
公共IList属性4{get;set;}
}
var jsonResponse=Newtonsoft.Json.JsonConvert.DeserializeObject(Response.Content)代码>
请在做出这些更改后发布您的观察结果。我可以向您推荐该网站。您可以在那里插入JSON响应,站点将基于它生成一个类构造 下面是我基于JSON创建的类:
public class Class1
{
// since your response json has camelCasing, you will need to define JsonProperty to represent camelCasing or just use public string property1 { get; set; } without any decoration.
[JsonProperty("property1")]
public string Property1 { get; set; }
[JsonProperty("property2")]
public string Property2 { get; set; }
[JsonProperty("property3")]
public EmptyClass Property3 { get; set; }
[JsonProperty("property4")]
public IList<string> Property4 { get; set; }
}
公共类属性3
{
}
公共类根对象
{
公共字符串属性1{get;set;}
公共字符串属性2{get;set;}
公共属性3属性3{get;set;}
公共列表属性4{get;set;}
}
属性3
不是字符串
它代表一个类。。。如果你真的想避开这个问题,只需创建一个空类,例如:public-class-Property3{}
,然后像这样声明它public-Property3-Property3{get;set;}
谢谢Codexer,现在我得到了正确的结果。谢谢Sam。这很有魅力。现在我得到了正确的结果。非常感谢您的时间和考虑。
var jsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<Class1>(Response.Content);
at Newtonsoft.Json.JsonTextReader.ParsePostValue(Boolean ignoreComments)
at Newtonsoft.Json.JsonTextReader.Read()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
public class Class1
{
// since your response json has camelCasing, you will need to define JsonProperty to represent camelCasing or just use public string property1 { get; set; } without any decoration.
[JsonProperty("property1")]
public string Property1 { get; set; }
[JsonProperty("property2")]
public string Property2 { get; set; }
[JsonProperty("property3")]
public EmptyClass Property3 { get; set; }
[JsonProperty("property4")]
public IList<string> Property4 { get; set; }
}
public class Property3
{
}
public class RootObject
{
public string property1 { get; set; }
public string property2 { get; set; }
public Property3 property3 { get; set; }
public List<string> property4 { get; set; }
}