1

我在使用 JSON.NET 反序列化某些数据时遇到问题。以下字符串虽然通过 JSON.NET 序列化,但不会正确反序列化。

{"ParentClause":null,"FilterClauseType":2,"FilterClauses":[ {"FilterClauseType":1,"FilterClauses":[],"ComparisonType":2,"FieldStaticName":"ContentType","Value": "隐藏","FieldValueType":2}, {"FilterClauseType":1,"FilterClauses":[],"ComparisonType":1,"FieldStaticName":"ContentType","Value":"Document","FieldValueType" :2}], "ComparisonType":null,"FieldStaticName":null,"Value":null,"FieldValueType":null}

这样做会JsonConvert.DeserializeObject()跳过FilterClauses数组的元素,即使它们明显存在。无论我使用方法的泛型还是非泛型重载,都会发生这种情况。

这是一个错误,还是有什么方法可以解决这个问题?我尝试实现一个 custom JsonConverter,但JReader传递给它的是抛出异常。

作为参考,这是类定义:

class FilterClause 
{
    public FilterClause ParentClause {get; set;}
    public FilterClauseType FilterClauseType {get; set;}
    public IList<FilterClause> FilterClauses {get; set;}
    public ComparisonType? ComparisonType {get; set;}
    public String FieldStaticName {get; set;}
    public String Value {get; set;}
    public FieldValueType? FieldValueType {get; set;}

    public FilterClause()
    {
        FilterClauses = ImmutableList.Create<FilterClause>();
    }
}
4

1 回答 1

0

试试这个

类定义

internal class SampleResponse1
{

    [JsonProperty("ParentClause")]
    public object ParentClause { get; set; }

    [JsonProperty("FilterClauseType")]
    public int FilterClauseType { get; set; }

    [JsonProperty("FilterClauses")]
    public FilterClaus[] FilterClauses { get; set; }

    [JsonProperty("ComparisonType")]
    public object ComparisonType { get; set; }

    [JsonProperty("FieldStaticName")]
    public object FieldStaticName { get; set; }

    [JsonProperty("Value")]
    public object Value { get; set; }

    [JsonProperty("FieldValueType")]
    public object FieldValueType { get; set; }
}

internal class FilterClaus
{

    [JsonProperty("FilterClauseType")]
    public int FilterClauseType { get; set; }

    [JsonProperty("FilterClauses")]
    public object[] FilterClauses { get; set; }

    [JsonProperty("ComparisonType")]
    public int ComparisonType { get; set; }

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

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

    [JsonProperty("FieldValueType")]
    public int FieldValueType { get; set; }
}

转换语法

var obj = JsonConvert.DeserializeObject<SampleResponse1>(Your_String);
于 2013-09-09T15:30:03.770 回答