【问题标题】:How to get a specific field from json object by key:value?如何通过key:value从json对象中获取特定字段?
【发布时间】:2021-11-26 07:33:24
【问题描述】:

我正在尝试使用 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 对象包中获取/删除特定对象?

【问题讨论】:

  • 欢迎使用 stackoverflow。到目前为止,您自己尝试过什么?你遇到了什么问题?你研究了什么?请编辑您的问题以包含更多信息。我推荐taking the tour,以及阅读how to ask a good questionwhat's on topic。提示:“使用 json 做某事”中的第一步总是:将其反序列化为正确的数据结构。
  • 感谢您的反馈和建议。下次我会试着提出一个好问题。
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: c# object elixir-jason


【解决方案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,

【讨论】:

  • 谢谢。!!!这正是我想在我的项目中实现的!!。
  • 欢迎 很高兴听到这个消息!如果可能的话,请喜欢我的回答,这将有助于我保持动力谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-23
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多