1

I am trying to extract data from D2L for the dropbox submissions using API's I have a link which returns a Json Array as per told on the documentation and from this Array I just need the Id feild nothing else.

I have tried to convert this Array into dynamic Object but that didn't help.

Here is my code.

var client = new RestClient("https://" + LMS_URL);
var authenticator = new ValenceAuthenticator(userContext);
string Link = "/d2l/api/le/1.12/UnitID/dropbox/folders/UniID/submissions/";
var request = new RestRequest(string.Format(Link));
request.Method = Method.GET;

authenticator.Authenticate(client, request);

var response = client.Execute(request);

string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(response.Content);
Response.Write(jsonString);

//var splashInfo = JsonConvert.DeserializeObject<ObjectD>(response.Content);
 dynamic jsonResponse = JsonConvert.DeserializeObject(response.Content);

var parsedObject = jsonResponse.Parse(jsonResponse);
string json = jsonResponse;
var popupJson = parsedObject["id"].ToString();

What my goal is that grab the list of ID's from this response and loop through them, these ID's are the key for my next API route.

Here is a glance at what I get from the response:

[
  {
    "Id": 2021,
    "CategoryId": null,
    "Name": "Graded Assignment:  Part 1",
    "CustomInstructions": {
      "Text": "Directions:\r\nCOMPLETE the following TestOut Activities\r\n\r\n1.2.10 Practice Questions\r\n1.3.8 Practice Questions\r\n\r\nGo to TestOut to complete the Assignment."
      //Other properties omitted
    }
    //Other properties omitted
  }
]
dbc
  • 104,963
  • 20
  • 228
  • 340
Maxi
  • 109
  • 1
  • 11
  • Why are you parsing the response *twice*? Also you sample JSON is not well-formed, it appears to be truncated after the value of `CustomInstructions.Text`. Upload it to https://jsonlint.com/ and you will get an error. Is your JSON really truncated, or did you omit the remainder from your question? – dbc Sep 06 '19 at 20:56
  • Hi @dbc The JSON has the ending `}` after the full-text note, Its a really big text I didn't want to post it al but yes it does closes with the proper `}` For the Parsing it twice, I am only doing it once, the second one is I using the same Parsing just adding what feild that I want "id" – Maxi Sep 06 '19 at 21:02
  • Are you sure the JSON shown in the question reflects the *top level schema* of the JSON returned? Your doc link says *This action retrieves a JSON array of EntityDropbox structures*, and [Dropbox.EntityDropbox](https://docs.valence.desire2learn.com/res/dropbox.html#Dropbox.EntityDropbox) doesn't look like what you show in your question. In fact it looks like [`Dropbox.DropboxFolder`](https://docs.valence.desire2learn.com/res/dropbox.html#Dropbox.DropboxFolder) which is returned by a different API. – dbc Sep 06 '19 at 21:15

1 Answers1

1

The outermost container in the JSON returned is an array, so you need to deserialize to a collection type as specified in the Newtonsoft's Serialization Guide: IEnumerable, Lists, and Arrays.

Since you only care about the Id property of the object(s) in the array, you can use JsonConvert.DeserializeAnonymousType to deserialize just the interesting value(s):

var ids =  JsonConvert.DeserializeAnonymousType(response.Content, new [] { new { Id = default(long) } })
    .Select(o => o.Id)
    .ToList();

And if you are certain the outermost array will contain exactly one item, you can do:

var id =  JsonConvert.DeserializeAnonymousType(response.Content, new [] { new { Id = default(long) } })
    .Select(o => o.Id)
    .Single();

Alternatively, if you think you will later need to deserialize additional properties, you could make an explicit data model as follows:

public class ObjectId
{
    public long Id { get; set; }
}

And do:

var ids =  JsonConvert.DeserializeObject<List<ObjectId>>(response.Content)
    .Select(o => o.Id)
    .ToList();

Notes:

  • You will need to determine from the API documentation whether long or int is more appropriate for the Id value.

  • As a general rule I recommend not parsing to dynamic because you lose compile-time checking for correctness. When dealing with completely free-form JSON, parsing to JToken may be a better solution -- but your JSON appears to have a fixed schema, so neither is necessary.

Demo fiddle here.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • This worked, thank you so much, the Demo helped alot as well, Thanks again! – Maxi Sep 06 '19 at 22:02
  • Hi @dbc I have a question about the above issue, I have a second object that I want to grab from this Json, how can I do so, I created the Class for the whole Json Object but it gives me an Error – Maxi Sep 12 '19 at 21:09
  • @Maxi - you should ask another question and include a [mcve] as well as the full `ToString()` output of the exception. I can't really tell the problem just from a comment. – dbc Sep 12 '19 at 22:45