I come back to you, because after having tried everything I can't manage to deserialize the following json:
{
"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"
}
It is a json obtained from the API of GLPI, for the connection no problem, I have my accesses I manage to recover the list in my _repository
CONTROLLERS
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>());
}
}
}
}
At the level of the model I think to have defined as it is necessary, the doubt is at the level of Datum!
MODELS
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
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;
}
}
}
So, as I understand, I can't get my json to deserialize and put it in my list.
I get the following message in Visual Studio in my breakpoint.
List list = _repository.GetAllList().ToList(); error CS0305: L'utilisation du type générique 'List' 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) 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.
Thank you in advance for your insights
CodePudding user response:
fix this
public Glpi GetAllList()
{
return JsonConvert.DeserializeObject<Glpi>(_savedFileGL
List<Glpi> list =PI);
}
and if you need only list of data you can fix action too
public ActionResult GLPI()
{
try
{
List<Glpi> list = _repository.GetAllList();
return View(list.Data);
}
CodePudding user response:
I can't figure out this code. I'm new to c#, but I think there's a typo, Thanks for your help
public Glpi GetAllList()
{
return JsonConvert.DeserializeObject<Glpi>(_savedFileGL
List<Glpi> list =PI);
}