Json.Net中的重复值

时间:2013-11-23 09:07:09

标签: c# asp.net json json.net

我正在使用Asp.Net Web API开展项目。此API将使用另一个API,我们称之为B点。

来自B点的响应将如下所示,并反序列化为Web API,因为我需要更改其中的一些信息,然后在Web API中再次对其进行序列化。

{
"Row": [
    {
        "key": "MjAxMy0xMQ==",
        "Cell": [
            {
                "column": "Y291bnQ6ZGFlbW9u",
                "timestamp": 1385195582067,
                "$": "MTE1"
            },
            {
                "column": "Y291bnQ6a2Vybg==",
                "timestamp": 1385195582067,
                "$": "MQ=="
            },
            {
                "column": "Y291bnQ6dXNlcg==",
                "timestamp": 1385195582067,
                "$": "MA=="
            }
        ]
    }
  ]
}

这是我的映射。

public class RowEntry
{
    [JsonProperty("Row")]
    public List<Row> Rows { get; set; }
}

public class Row
{
    [JsonProperty("key")]
    private string _key;
    public string Key
    {
        get { return Base64Converter.ToUTF8(_key); }
        set
        {
            _key = value;
        }
    }

    [JsonProperty(PropertyName = "Cell")]
    public List<Cell> Cells { get; set; }
}

public class Cell
{
    [JsonProperty(PropertyName = "column")]
    private string _column;
    public string Column
    {
        get
        {
            return Base64Converter.ToUTF8(_column);
        }

        set
        {
            _column = value;
        }
    }

    [JsonProperty(PropertyName = "timestamp")]
    public string Timestamp { get; set; }

    [JsonProperty(PropertyName = "$")]
    private string _value;
    public string Value
    {
        get
        {
            return Base64Converter.ToUTF8(_value);
        }
        set
        {
            _value = value;
        }
    }
}

在我的控制器中我有

// Get JSON from point-B
var response = await _client.GetStringAsync(BuildUri(table, startDate, endDate));
// Parse the result into RowEntry object
var results = JsonConvert.DeserializeObject<RowEntry>(response);
// This should return the RowEntry object as JSON as I'm using Web API here
return results;

这应该返回一个新的JSON,它只包含我上面的类的元素。但是,它似乎也包含来自B点的第一个请求的JSON。

{
"Row": [
    {
        "Key": "decoded value here",
        "key": "MjAxMy0xMQ==",
        "Cell": [
            {
                "Column": "decoded value here",
                "column": "Y291bnQ6ZGFlbW9u",
                "timestamp": 1385195582067,
                "Value": "decoded value here",
                "$": "MTE1"
            },
            // abbreviated
        ]
    }
  ]
}

我很困惑,因为我正在序列化RowEntry,我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:2)

您可以通过将[JsonIgnore]添加到已使用[JsonProperty]装饰相应私有成员变量的公共属性来解决此问题。 Json.Net并不隐含地知道特定的私有成员变量对应于特定的公共属性,因此在序列化时它将包括两者,除非你告诉它不要。

修订课程:

public class Row
{
    [JsonProperty("key")]
    private string _key;
    [JsonIgnore]
    public string Key
    {
        get { return Base64Converter.ToUTF8(_key); }
        set { _key = value; }
    }

    [JsonProperty(PropertyName = "Cell")]
    public List<Cell> Cells { get; set; }
}

public class Cell
{
    [JsonProperty(PropertyName = "column")]
    private string _column;
    [JsonIgnore]
    public string Column
    {
        get { return Base64Converter.ToUTF8(_column); }
        set { _column = value; }
    }

    [JsonProperty(PropertyName = "timestamp")]
    public string Timestamp { get; set; }

    [JsonProperty(PropertyName = "$")]
    private string _value;
    [JsonIgnore]
    public string Value
    {
        get { return Base64Converter.ToUTF8(_value); }
        set { _value = value; }
    }
}