从 JSON 数组中提取几个值

Extract few values from JSON Array

我面临的问题是我在 JSON 中要求的值得到 0。

我最近问了一个问题 关于从来自 API 的 JSON 数组响应中获取特定值的问题。

现在我有一个来自不同路线的新 JSON 响应,我想要一个值及其称为 EntityId 它在实体块中的位置请参阅代码以获取更多详细信息,我想抓住从这个响应并将它们放在列表或数组中,因为它告诉我谁在 D2L 中提交了作业。

更新- 在收到用户的评论后,我使用 JSON to C# 工具创建了包含所有值的完整 class,但我仍然得到值 0,0,0,0 .

现在我得到的是 0,0,0,0 值,如果可能的话,我没有得到实际值,我想获取 Entity 上的整个块。

JSON

[
  {
    "Entity": {
      "DisplayName": "FullName",
      "EntityId": 123,
      "EntityType": "User",
      "Active": true
    },
    "Status": 1,
    "Feedback": null,
    "Submissions": [
      {
        "Id": 27,
        "SubmittedBy": {
          "Identifier": "123",
          "DisplayName": "FullName"
        },
        "SubmissionDate": "2019-08-01T15:25:04.130Z",
        "Comment": {
          "Text": "",
          "Html": ""
        },
        "Files": [
          {
            "IsRead": false,
            "IsFlagged": false,
            "IsDeleted": false,
            "FileId": 1245,
            "FileName": "1.2.10.png",
            "Size": 144407
          },
          {
            "IsRead": false,
            "IsFlagged": false,
            "IsDeleted": false,
            "FileId": 292,
            "FileName": "1.3.8.png",
            "Size": 127869
          }
        ]
      }
    ],
    "CompletionDate": "2019-08-01T15:25:04.130Z"
  },
  {
    "Entity": {
      "DisplayName": "FullName",
      "EntityId": 123,
      "EntityType": "User",
      "Active": true
    },
    "Status": 1,
    "Feedback": null,
    "Submissions": [
      {
        "Id": 41,
        "SubmittedBy": {
          "Identifier": "123",
          "DisplayName": "FullName"
        },
        "SubmissionDate": "2019-08-03T03:31:43.807Z",
        "Comment": {
          "Text": " \nAlex",
          "Html": "<p></p>\n<p>Alex</p>"
        },
        "Files": [
          {
            "IsRead": false,
            "IsFlagged": false,
            "IsDeleted": false,
            "FileId": 313,
            "FileName": "Capture 1.2.10 Questions.PNG",
            "Size": 97722
          }
        ]
      }
    ],
    "CompletionDate": "2019-08-03T03:31:43.807Z"
  }
]

类:

public class Entity
{
    public string DisplayName { get; set; }
    public int EntityId { get; set; }
    public string EntityType { get; set; }
    public bool Active { get; set; }
}

public class SubmittedBy
{
    public string Identifier { get; set; }
    public string DisplayName { get; set; }
}

public class Comment
{
    public string Text { get; set; }
    public string Html { get; set; }
}

public class File
{
    public bool IsRead { get; set; }
    public bool IsFlagged { get; set; }
    public bool IsDeleted { get; set; }
    public int FileId { get; set; }
    public string FileName { get; set; }
    public int Size { get; set; }
}

public class Submission
{
    public int Id { get; set; }
    public SubmittedBy SubmittedBy { get; set; }
    public DateTime SubmissionDate { get; set; }
    public Comment Comment { get; set; }
    public IList<File> Files { get; set; }
}

public class Example
{
    public Entity Entity { get; set; }
    public int Status { get; set; }
    public object Feedback { get; set; }
    public IList<Submission> Submissions { get; set; }
    public DateTime CompletionDate { get; set; }
}

代码:

var request = new RestRequest(string.Format(Link));
request.Method = Method.GET;

authenticator.Authenticate(client, request);

var response = client.Execute(request);

var thisasa = JsonConvert.DeserializeObject<List<Example>> 
   (response.Content).Select(
    o => o.Identifier).ToList();

您问题中显示的 Example 数据模型已可用于成功反序列化显示的 JSON。剩下的就是通过 Select:

选择 Entity.EntityId 属性
var thisasa = JsonConvert.DeserializeObject<List<Example>>(response.Content)
    .Select(o => o.Entity.EntityId)
    .ToList();

演示 fiddle #1 here.

顺便说一句,如果您需要Entity.EntityId,您可以按如下方式简化您的数据模型:

public class Entity
{
    public int EntityId { get; set; }
}

public class Example
{
    public Entity Entity { get; set; }
}

演示 fiddle #2 here.

(顺便说一句,由于您的 Example class 对应于记录的对象 Dropbox.EntityDropbox,为了清楚起见,您可能需要将类型重命名为 EntityDropbox。)