从Json对象中删除Children

时间:2018-02-20 07:26:06

标签: json c#-4.0 serialization json.net json-deserialization

我有一个Serialized JsonObject,我必须从其中一个子元素中删除除一个json元素或数组之外的所有元素。

{
"Index": "1",
"DoCID": "",
"Sin_id": "{xxxx}",
"Loan_LastModified": "2015-03-30T08:31:20-04:00",
"Col_1": null,
"col_2": null,
"ChildPair": [{
    "Index": 1,
    "Fields_35": "",
    "Fields_52": "",
    "Fields_1811": "PrimaryResidence",
    "Fields_4000": "discount point",
    "Fields_4077": null
}, {
    "Index": 2,
    "Fields_35": "",
    "Fields_52": "",
    "Fields_1811": "PrimaryResidence",
    "Fields_4000": "discount point",
    "Fields_4077": null
}, {
    "Index": 3,
    "Fields_35": "",
    "Fields_52": "",
    "Fields_1811": "PrimaryResidence",
    "Fields_4000": "discount point",
    "Fields_4077": null
},{
    "Index": 4,
    "Fields_35": "",
    "Fields_52": "",
    "Fields_1811": "PrimaryResidence",
    "Fields_4000": "discount point",
    "Fields_4077": null
}]

} 在childPair中我需要删除除第一个之外的所有索引。 请帮忙。 感谢

1 个答案:

答案 0 :(得分:1)

如果我理解正确,这样的事情应该有效。我正在删除所有索引大于1的ChildPair对象。如果需要,可以更改谓词以执行类似的任务

class RootClass
{
    public string Index { get; set; }
 [JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
    public string DoCID { get; set; }
    public string Sin_id { get; set; }
    public DateTime Loan_LastModified { get; set; }
    public object Col_1 { get; set; }
    public object col_2 { get; set; }
    public List<ChildPair> ChildPair { get; set; }
}

class ChildPair
{
    public int Index { get; set; }
    public string Fields_35 { get; set; }
    public string Fields_52 { get; set; }
    public string Fields_1811 { get; set; }
    public string Fields_4000 { get; set; }
    public object Fields_4077 { get; set; }
}

var json = @"{
                    ""Index"": ""1"",
                    ""DoCID"": """",
                    ""Sin_id"": ""{xxxx}"",
                    ""Loan_LastModified"": ""2015-03-30T08:31:20-04:00"",
                    ""Col_1"": null,
                    ""col_2"": null,
                    ""ChildPair"": [{
                        ""Index"": 1,
                        ""Fields_35"": """",
                        ""Fields_52"": """",
                        ""Fields_1811"": ""PrimaryResidence"",
                        ""Fields_4000"": ""discount point"",
                        ""Fields_4077"": null
                    }, {
                        ""Index"": 2,
                        ""Fields_35"": """",
                        ""Fields_52"": """",
                        ""Fields_1811"": ""PrimaryResidence"",
                        ""Fields_4000"": ""discount point"",
                        ""Fields_4077"": null
                    }, {
                        ""Index"": 3,
                        ""Fields_35"": """",
                        ""Fields_52"": """",
                        ""Fields_1811"": ""PrimaryResidence"",
                        ""Fields_4000"": ""discount point"",
                        ""Fields_4077"": null
                    },{
                        ""Index"": 4,
                        ""Fields_35"": """",
                        ""Fields_52"": """",
                        ""Fields_1811"": ""PrimaryResidence"",
                        ""Fields_4000"": ""discount point"",
                        ""Fields_4077"": null
                    }]}";

var res = JsonConvert.DeserializeObject<RootClass>(json);

res.ChildPair.RemoveAll(childPair => childPair.Index != 1);

如果要跳过null属性(比如DocID),可以尝试:

var output = JsonConvert.SerializeObject(rootObject, 
                        Newtonsoft.Json.Formatting.None, 
                        new JsonSerializerSettings { 
                            NullValueHandling =NullValueHandling.Ignore
                        });

如果为null,这将从输出中删除DocID或Sin_Id。

..并从输出中删除DocID和Sin_id,无论它们是否为空:

using Newtonsoft.Json.Linq;
JObject rootObject = JObject.Parse(json);

rootObject.Property("DoCID").Remove();
rootObject.Property("Sin_id").Remove();

var output = JsonConvert.SerializeObject(rootObject);

Console.WriteLine("Output: " + output);