如何反序列化 json 响应

How to deserialize json response

[
    {
        "property1": "prop-00000001",
        "property2": "property2value1",
        "property3": {},
        "property4": [
            "Prop4-00000001",
            "Prop4-00000002"
        ]
    },
    {
        "property1": "prop-00000002",
        "property2": "property2value2",
        "property3": {},
        "property4": [
            "Prop4-00000003",
            "Prop4-00000004"
        ]
    }
]

我将收到如上所示的 json 响应。项目的数量可能会增加,例如现在有 2 个,它可能会增加,具体取决于数据库中的记录数。另一点是上面显示的每个 属性 的值将始终与上面的格式相似。

我的问题 是当我如下使用 class 来反序列化 json 响应时,它不起作用:

public class Class1
{
    [JsonProperty("Property1")]
    public string Property1 { get; set; }

    [JsonProperty("Property2")]
    public string Property2 { get; set; }

    [JsonProperty("Property3")]
    public string Property3 { get; set; }

    [JsonProperty("Property4")]
    public IList<string> Property4 { get; set; }
}

当我这样做时:

var jsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<Class1>(Response.Content);

异常引发为:

Exception message: After parsing a value an unexpected character was encountered: ". Path 'property3', line 1, position 60.

这意味着它无法忽略作为 属性3 值的大括号:即 {}.

堆栈跟踪是:

   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)

如何编写正确的 C# class 或 C# 解决方案?

  1. Property3 不是字符串。它的对象。您的回复 json 显示空对象 ({})。所以,定义一个class like

    public class EmptyClass { // Add properties to this class based on your response. }

  2. 此外,您的 属性 名称和 JsonProperty 名称与响应大小写不匹配。所以修改Class1如下:

`

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; }
}
  1. 纠正以上两个后,由于您的响应json是集合,您还需要反序列化为集合(List< Class1 >)。那么,您可以尝试使用 var jsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Class1>>(Response.Content);.

请 post 您在进行这些更改后的意见。

我可以向您推荐网站http://json2csharp.com。您可以在那里插入您的 JSON 响应,网站将基于它生成一个 class 结构。

这是我根据你的 JSON 创建的 class:

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; }
}