-1

我正在尝试使用 json 保存我的数据。我不知道如何从一组 json 对象中获取特定的对象组。

[
{
  "dateinformation": "2021-10-05:23:01",
  "id": 1,
  "MoveCount": 3,
  "StartPoint": 2322,
  "TotalDist": 2331,
  "SafeDist": 21332,
  "Feed": 2332,
  "EndPoint":23245,
  "Count":1221,

},
{

  "dateinformation": "2021-10-05:26:01",
  "id": 2,
  "MoveCount": 3,     
  "StartPoint": 2322,
  "TotalDist": 2331,
  "SafeDist": 21332,
  "Feed": 2332,
  "EndPoint":23245,
  "Count":1221,
},
{

  "dateinformation": "2021-10-55:03:01",
  "id": 3,
  "MoveCount": 3,
  "StartPoint": 2322,
  "TotalDist": 2331,
  "SafeDist": 21332,
  "Feed": 2332,
  "EndPoint":23245,
  "Count":1221,

}]

是否可以通过其“id”值从 json 对象包中获取/删除特定对象?

4

1 回答 1

1

那样做

命名空间将包括:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

在这里你可以用 specificId 替换你的 id

 List<Root> myDeserializedClass = JsonConvert.DeserializeObject<List<Root>>(myJsonResponse); // myJsonResponse is My Source Json
        int specificId = 123; // my dynamic
        var specficObject = myDeserializedClass.Where(x => x.id == specificId);

反序列化 JSON 响应的类:

     public class Root
        {
            public string dateinformation { get; set; }
            public int id { get; set; }
            public int MoveCount { get; set; }
            public int StartPoint { get; set; }
            public int TotalDist { get; set; }
            public int SafeDist { get; set; }
            public int Feed { get; set; }
            public int EndPoint { get; set; }
            public int Count { get; set; }
        }

另外,我注意到你的 JSON 最后一列逗号不应该像“计数”:1221,

于 2021-10-06T05:51:38.963 回答