Home > OS >  get a JSON token in C#
get a JSON token in C#

Time:07-21

I get the following JSON file by accessing a REST API in C#

[{
        "id": 71,
        "parent_id": 0,
        "name": "Espace priv\u00e9",
        "can_read": true,
        "can_download": true,
        "can_write": true,
        "can_edit": true,
        "can_delete": true,
        "can_share": true,
        "creation": "2022-07-12T09:53:34 00:00",
        "modification": "2022-07-12T09:53:34 00:00",
        "owner": "Sylvain KRIER",
        "owner_id": "23"
    }, {
        "id": 80,
        "parent_id": 0,
        "name": "CLI12",
        "can_read": true,
        "can_download": true,
        "can_write": true,
        "can_edit": true,
        "can_delete": true,
        "can_share": true,
        "creation": "2022-07-13T07:43:25 00:00",
        "modification": "2022-07-13T08:25:46 00:00",
        "owner": "Patrimoine Click",
        "owner_id": "2",
        "flags": 2
    }, {
        "id": 53,
        "parent_id": 0,
        "name": "DOCUMENTATION",
        "can_read": true,
        "can_download": true,
        "can_write": true,
        "can_edit": true,
        "can_delete": true,
        "can_share": true,
        "creation": "2022-06-30T14:38:55 00:00",
        "modification": "2022-06-30T14:39:05 00:00",
        "owner": "Patrimoine Click",
        "owner_id": "2",
        "flags": 2
    }, {
        "id": 77,
        "parent_id": 0,
        "name": "Mme AMSELLEM Smadar",
        "can_read": true,
        "can_download": true,
        "can_write": true,
        "can_edit": true,
        "can_delete": true,
        "can_share": true,
        "creation": "2022-07-12T10:33:29 00:00",
        "modification": "2022-07-12T10:33:42 00:00",
        "owner": "Patrimoine Click",
        "owner_id": "2",
        "flags": 2
    }, {
        "id": 68,
        "parent_id": 0,
        "name": "Mr NGUIMFACK Guy",
        "can_read": true,
        "can_download": true,
        "can_write": true,
        "can_edit": true,
        "can_delete": true,
        "can_share": true,
        "creation": "2022-07-12T08:41:33 00:00",
        "modification": "2022-07-12T13:28:06 00:00",
        "owner": "Patrimoine Click",
        "owner_id": "2",
        "flags": 2
    }, {
        "id": 83,
        "parent_id": 0,
        "name": "vergne-CLI1",
        "can_read": true,
        "can_download": true,
        "can_write": true,
        "can_edit": true,
        "can_delete": true,
        "can_share": true,
        "creation": "2022-07-13T08:20:06 00:00",
        "modification": "2022-07-13T08:23:29 00:00",
        "owner": "Sylvain KRIER",
        "owner_id": "23",
        "flags": 2
    }, {
        "id": 110,
        "parent_id": 0,
        "name": "krier1",
        "can_read": true,
        "can_download": true,
        "can_write": true,
        "can_edit": true,
        "can_delete": true,
        "can_share": true,
        "creation": "2022-07-21T08:57:35 00:00",
        "modification": "2022-07-21T08:57:35 00:00",
        "owner": "Sylvain KRIER",
        "owner_id": "23",
        "flags": 2
    }
]

It's a folder list. I need to get the "id" of each folder with the "name"

I try to deserialize the JSON string like this, but it doesn't work :

var a = JObject.Parse(((RestSharp.RestResponseBase)resultat.Value).Content).SelectToken("id").ToList();

((RestSharp.RestResponseBase)resultat.Value).Content is the JSON string mentioned

Thanks for giving me help.

CodePudding user response:

Personally, I always deserialize my JSON to a statically typed object before doing anything else with it, in this case you can just create a mostly empty JsonDirectory class with just the id property and deserialize to a list of it, like so

public class JsonDirectory
{
    [JsonPropertyName("id")]
    public int Id { get; set; }
}

// Deserialize
var json = "...";
var jsonDirectory = JsonSerializer.Deserialize<List<JsonDirectory>>(json);

foreach (var jd in jsonDirectory)
    Console.WriteLine(jd.Id);

Here's a demo showcasing his working on your example JSON


P.S this answer uses System.Text.Json, if you're using Newtonsoft.Json then you'll need to change out [JsonPropertyName("id")] with [JsonProperty("id")] and the deserialization line to JsonConvert.DeserializeObject<List<JsonDirectory>>(json)

CodePudding user response:

you can use linq to create a list

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

List<IdName> idNameList = JArray.Parse(json)
   .Select(j => new IdName { Id = (int)j["id"], Name = (string)j["name"] }).ToList();

public class IdName
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("name")]
    public int Name { get; set; }
}

result

[
  {
    "Id": 71,
    "Name": "Espace privé"
  },
  {
    "Id": 80,
    "Name": "CLI12"
  },
  {
    "Id": 53,
    "Name": "DOCUMENTATION"
  },
  {
    "Id": 77,
    "Name": "Mme AMSELLEM Smadar"
  },
  {
    "Id": 68,
    "Name": "Mr NGUIMFACK Guy"
  },
  {
    "Id": 83,
    "Name": "vergne-CLI1"
  },
  {
    "Id": 110,
    "Name": "krier1"
  }
]
  • Related