猿问

如何有选择地从现有 JSON 字符串中删除某些属性?

我有一个嵌套的 JSON 字符串。我想减小 JSON 的大小,仅选择部分属性并忽略其余属性。我该怎么做?


JSON 字符串示例:


[

    {

        "ID": 17,

        "Code": "1",

        "SupplierID": 24,

        "Date": "2018-07-04T10:16:37.107",

        "OrderDetails": [

            {

                "ID": 17,

                "OrderID": 17,

                "ItemID": 5806,

                "Quantity": 20000.0,

                "ConfirmQuantity": 20000.0,

                "ConfirmDate": "2018-09-11T11:33:30.463",

                "Deadline": "2018-07-22T00:00:00",

                "IsCanceled": false,

                "PersonnelID": 667,

                "IsConfirmed": false

            },

            {

                "ID": 190,

                "OrderID": 170,

                "ItemID": 5806,

                "Quantity": 20000.0,

                "ConfirmQuantity": 20000.0,

                "ConfirmDate": "2018-09-11T11:33:30.463",

                "Deadline": "2018-07-22T00:00:00",

                "IsCanceled": false,

                "PersonnelID": 6670,

                "IsConfirmed": false

            }

        ]

    },

    {

        "ID": 19,

        "Code": "2",

        "SupplierID": 20,

        "Date": "2018-07-14T13:25:50.030",

        "OrderDetails": [

            {

                "ID": 18,

                "OrderID": 19,

                "ItemID": 15425,

                "Quantity": 2100.0,

                "ConfirmQuantity": 2100.0,

                "Deadline": "2018-07-18T00:00:00",

                "IsCanceled": false,

                "PersonnelID": 445,

                "IsConfirmed": false

            }

        ]

    }

]

期望的结果:


[

    {

        "ID": 17,

        "Code": "1",

        "OrderDetails": [

            {

                "ID": 17,

                "ItemID": 5806,

                "Quantity": 20000.0,

            },

            {

                "ID": 190,

                "ItemID": 5806,

                "Quantity": 20000.0,

            }

        ]

    },



噜噜哒
浏览 110回答 2
2回答

达令说

您可以使用 Json.Net 的LINQ-to-JSON API 执行以下操作:var namesToKeep = new string[] { "ID", "Code", "OrderDetails", "ItemID", "Quantity" };var jArray = JArray.Parse(jsonString);foreach (var prop in jArray.Descendants().OfType<JProperty>().ToList()){    if (!namesToKeep.Contains(prop.Name))        prop.Remove();}jsonString = jArray.ToString();小提琴: https: //dotnetfiddle.net/Pj9Wsu

MMTTMM

为了从序列化中排除属性,您需要使用[JsonIgnore]attribute 来标记属性。像这样的东西:public class OrderDetail{&nbsp; &nbsp; public int ID { get; set; }&nbsp; &nbsp; public int OrderID { get; set; }&nbsp; &nbsp; public int ItemID { get; set; }&nbsp; &nbsp; [JsonIgnore]&nbsp; &nbsp; public double Quantity { get; set; }&nbsp; &nbsp; [JsonIgnore]&nbsp; &nbsp; public double ConfirmQuantity { get; set; }&nbsp; &nbsp; [JsonIgnore]&nbsp; &nbsp; public DateTime ConfirmDate { get; set; }&nbsp; &nbsp; [JsonIgnore]&nbsp; &nbsp; public DateTime Deadline { get; set; }&nbsp; &nbsp; [JsonIgnore]&nbsp; &nbsp; public bool IsCanceled { get; set; }&nbsp; &nbsp; [JsonIgnore]&nbsp; &nbsp; public int PersonnelID { get; set; }&nbsp; &nbsp; [JsonIgnore]&nbsp; &nbsp; public bool IsConfirmed { get; set; }}public class RootObject{&nbsp; &nbsp; public int ID { get; set; }&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; [JsonIgnore]&nbsp; &nbsp; public int SupplierID { get; set; }&nbsp; &nbsp; [JsonIgnore]&nbsp; &nbsp; public DateTime Date { get; set; }&nbsp; &nbsp; public List<OrderDetail> OrderDetails { get; set; }}但我实际上建议您将原始集合投影到具有属性子集的对象集合中。最简单的方法是仅使用要序列化的属性定义新类,并将其从一个类映射到另一个类(使用普通 LINQSelect或某种映射器):public class OrderDetailModel{&nbsp; &nbsp; public int ID { get; set; }&nbsp; &nbsp; public int OrderID { get; set; }&nbsp; &nbsp; public int ItemID { get; set; }}public class RootObjectModel{&nbsp; &nbsp; public int ID { get; set; }&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public List<OrderDetailModel> OrderDetails { get; set; }}
随时随地看视频慕课网APP
我要回答