如何根据C#中的条件删除Json对象集?

时间:2016-11-28 05:27:00

标签: c# asp.net json

我有一个Json文件,其中包含以下数据:

[
  {
    "applicationName": "ABC",
    "machineName": "XYZ",
    "OrderNumber": "N46464646"
  },
  {
    "applicationName": "DEF",
    "machineName": "XYZ",
    "OrderNumber": "D46364636"
  },
  {
    "applicationName": "ABC",
    "machineName": "XYZ",
    "OrderNumber": "D34343434"
  }
]

我想根据订单号从上述数据中删除一组对象,例如:如果我给订单号“D46364636”,它应该删除 -

{
    "applicationName": "DEF",
    "machineName": "XYZ",
    "OrderNumber": "D46364636"
}

我试过这样做 -

JsonDataList = new List<JsonItem>();
                string input = "";
                string inputApplicationName = "";
                string inputMachineName = "";
                string inputOrderNum = "";
                while (input != "q")
                {
                    Console.WriteLine("Press 'd' to Delete item");
                    Console.WriteLine("Press 'q' to Quit Program");
                    Console.WriteLine("Press Command:");
                    input = Console.ReadLine();
                    switch (input)
                    {
                        case "d":
                            Console.WriteLine("Enter Order Number to remove:");
                            inputOrderNum = Console.ReadLine();
                            var js = File.ReadAllText(@"C:\Users\ab54253\Documents\visual studio 2010\Projects\JSONExample\JSONExample\JsonData\AutomatedJson.json");
                            var result=JsonConvert.DeserializeObject<List<JsonData>>(js);
                            foreach(var item in result)
                            {
                                if(item.OrderNumber==inputOrderNum)
                                {
                                    JsonDataList.Remove(new JsonItem(item.applicationName, item.machineName, item.OrderNumber));
                                }
                            }
                            break;
                        case "q":
                            Console.WriteLine("Quit Program");
                            break;
                        default:
                            Console.WriteLine("Incorrect Command, Try Again");
                            break;
                    }
                }
                Console.WriteLine("Rewriting AutomatedJson.json");
                string JD = JsonConvert.SerializeObject(JsonDataList, Newtonsoft.Json.Formatting.Indented);
                File.WriteAllText(@"C:\Users\ab54253\Documents\visual studio 2010\Projects\JSONExample\JSONExample\JsonData\AutomatedJson.json", JD);
                Console.ReadLine();

我可以检查条件,我曾经使用以下方法删除数据:

if(item.OrderNumber==inputOrderNum)
{
  JsonDataList.Remove(new JsonItem(item.applicationName, item.machineName, item.OrderNumber));
}

通过使用上面的代码,它将从JSON文件中删除所有数据,而不是删除所需的数据。请指导我。

1 个答案:

答案 0 :(得分:1)

您可以使用反序列化字符串到对象中。然后使用Linq选择想要的项目,然后再次使用Json.NET获取JSON字符串。

    public class Item
    {
       public string applicationName {get; set;}
       public string machineName {get;set;}
       public string OrderNumber {get; set;}
    }

var items = JsonConvert.DeserializeObject<List<Item>>(JsonString);

var newJsonString = JsonConvert.SerializeObject(items.Where(i => i.OrderNumber != "D46364636"));