Bonjour à tous,

Je revient vers vous, car après avoir tout essayé je n'arrive pas à déserializer le json suivant :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
    "totalcount": 2,
    "count": 2,
    "sort": "1",
    "order": "ASC",
    "data": [
        {
            "1": "S011908",
            "80": "Root entity",
            "31": "Hors-service > En attente réinitialisation",
            "70": "TARTUF",
            "id": 1333,
            "itemtype": "Computer"
        },
        {
            "1": "S01B2101",
            "80": "Root entity",
            "31": "Fonctionnel > Utilisé",
            "70": "FILOU",
            "id": 1360,
            "itemtype": "Computer"
        }
    ],
    "content-range": "0-1/2"
}
Il s'agit d'un json obtenu depuis l' API de GLPI, pour la connexion pas de problème, j'ai mes accès j'arrive à récupérer la liste dans mon _repository

CONTROLLERS
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using Portail_DIN.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
 
namespace Portail_DIN.Controllers
{
    public class GLPIController : Controller
    {
        private readonly GLPIJSONRepository _repository;
        private WebClient client = new WebClient();
        private string session_token;
        public GLPIController()
        {
            client.UseDefaultCredentials = true;
            client.Headers.Add("App-Token", "test");
            client.Headers.Add("Authorization", "Basic test");
            string path = client.DownloadString("https://IP/glpi/apirest.php/initSession");
            session_token = path.Substring(18, path.LastIndexOf("\"") - 18); 
 
            client.Headers.Add("session-token", session_token);// "");
            client.Headers.Add(HttpRequestHeader.Accept, "application/json");
            client.Encoding = Encoding.UTF8;
            string json = client.DownloadString("https://IP/glpi/apirest.php/search/AllAssets/?is_deleted=0&as_map=0&criteria[0][link]=AND&criteria[0][field]=70&criteria[0][searchtype]=contains&criteria[0][value]=Normand");
            //View(json);
            _repository = new GLPIJSONRepository(json);
        }
 
 
        // GET: GLPI
        public ActionResult GLPI()
        {
            try
            {
                List<Glpi> list = _repository.GetAllList().ToList();
                return View(list);
            }
            catch
            {
                return View(new List<Glpi>());
            }
        }
    }
}
Au niveau du model je pense avoir défini comme il le faut, le doute est au niveau du Datum !

MODELS
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
 
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
 
namespace Portail_DIN.Models
{
    public partial class Glpi
    {
        [JsonProperty("totalcount")]
        public long Totalcount { get; set; }
 
        [JsonProperty("count")]
        public long Count { get; set; }
 
        [JsonProperty("sort")]
        public long Sort { get; set; }
 
        [JsonProperty("order")]
        public string Order { get; set; }
 
        [JsonProperty("data")]
        public Datum[] Data { get; set; }
 
        [JsonProperty("content-range")]
        public string ContentRange { get; set; }
    }
 
    public partial class Datum
    {
        [JsonProperty("1")]
        public string Nommachine { get; set; }
 
        [JsonProperty("31")]
        public string Statut { get; set; }
 
        [JsonProperty("70")]
        public string Utilisateur { get; set; }
 
        [JsonProperty("80")]
        public string Entite { get; set; }
 
        [JsonProperty("id")]
        public long Id { get; set; }
 
        [JsonProperty("itemtype")]
        public string Itemtype { get; set; }
    }
}
MODELS
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using System.IO;
 
namespace Portail_DIN.Models
{
    /// <summary>
    /// permet de gérer les articles qui sont enregistrés dans un fichier JSON
    /// </summary>
    public class GLPIJSONRepository
    {
        /// <summary>
        /// Représente le chemin du fichier JSON
        /// </summary>
        private readonly string _savedFileGLPI;
 
        /// <summary>
        /// Construit le gestionnaire d'article à partir du nom d'un fichier JSON
        /// </summary>
        /// <param name="fileName">nom du fichier json</param>
        public GLPIJSONRepository(string fileName)
        {
            _savedFileGLPI = fileName;
        }
 
        /// <summary>
        /// Obtient une liste de tout les articles
        /// </summary>
        public IEnumerable<Glpi> GetAllList()
        {
            List<Glpi> list = JsonConvert.DeserializeObject<List<Glpi>>(_savedFileGLPI);
            return list;
        }
    }
}
Donc, comme je comprends, je n'arrive pas à deserializer mon json pour le mettre dans ma liste.

J'obtiens le message suivant dans Visual Studio dans mon point d'arrêt.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
		List<Glpi> list = _repository.GetAllList().ToList();
error CS0305: L'utilisation du type générique 'List<T>' nécessite des arguments de type 1
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Portail_DIN.Models.Glpi]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'totalcount', line 1, position 14.
Merci d'avance pour vos lumières