Search code examples
c#asp.netarraysjsondesire2learn

Extract few values from JSON Array


The problem I am facing is that I am getting 0's for the values I ask for in the JSON.

I recently asked a question here about getting specific values from a JSON array response that come from an API.

Now I have a new JSON response from a different route and in there I want one values and its called EntityId its places in a block of Entity See the code for more details, I would like to grab that from this response and place them in a list or Array because it tells me who submitted the assignment in D2L.

UPDATE- After getting comments from a user I used JSON to C# tool to create the full class with all the values but I still get the values as 0,0,0,0.

Right now I am getting 0,0,0,0 values and not getting the actual values if it's possible I would like to grab the whole block on 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"
  }
]

Classes:

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; }
}

Code:

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();

Solution

  • The Example data model shown in your question already can be used to successfully deserialize the JSON shown. All that's left is to pick out the Entity.EntityId property via a Select:

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

    Demo fiddle #1 here.

    Incidentally, if you only need Entity.EntityId you could simplify your data model as follows:

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

    Demo fiddle #2 here.

    (As an aside, since your Example class corresponds to the documented object Dropbox.EntityDropbox, you might want to rename your type to EntityDropbox for clarity.)