如何反序列化json响应

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

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

我的问题是当我使用下面的类来反序列化 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);

异常引发为:

异常消息:解析值后遇到意外字符:“。路径 'property3',第 1 行,位置 60。

这意味着它无法忽略作为 property3 值的大括号:即 {}。

堆栈跟踪是:

   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# 类或 C# 解决方案?

山姆
  1. Property3 不是字符串。它的对象。您的响应 json 显示空对象 ({})。所以,定义一个类

    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);.

请在进行这些更改后发布您的观察结果。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章