tengo un archivo JSON y me gustaria sacar todos los datos de ahí y guardarlos en variables para luego tratarlos.
{
"codcli": " 17",
"nomcli": "BICICLETA ASTURIANA, S.L.",
"codrep": " 4",
"telcli": "900.222.113",
"e_mail": "[email protected]"
}
Este seria un ejemplo de lo que hay en el JSON. Mi proyecto es uno de consola de C#.
Tengo la clase json con esto puesto.
class Cliente
{
public Clientes[] clientes { get; set; }
}
class Clientes
{
public string codcli { get; set; }
public string nomcli { get; set; }
public string codrep { get; set; }
public string telcli { get; set; }
public string e_mail { get; set; }
}
Mi clase principal es esta
private static void leerJSON()
{
StreamReader r = new StreamReader("file.json");
string jsonString = r.ReadToEnd();
var m = JsonConvert.DeserializeObject<Cliente>(jsonString.Replace("\n", "").Replace(" ", ""));
string codigo = m.clientes[0].ToString();
string nombre = m.clientes[1].ToString();
string representante = m.clientes[2].ToString();
string telefono = m.clientes[3].ToString();
string email = m.clientes[4].ToString();
Console.WriteLine(codigo + " " + nombre + " " + representante + " " + telefono + " " + email);
Console.ReadLine();
}
La variable jsonString recoge todos los campos =
jsonString "[\n {\n \"codcli\": \" 1\",\n \"nomcli\": \"SPORTS ABC\",\n \"codrep\": \" 1\",\n \"telcli\": \"93.345.67.89\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 2\",\n \"nomcli\": \"SPORTI, S.A.\",\n \"codrep\": \" 4\",\n \"telcli\": \"93.222.11.23\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 3\",\n \"nomcli\": \"DEPORTES REUNIDOS, S.L.\",\n \"codrep\": \" 1\",\n \"telcli\": \"91.234.11.33\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 4\",\n \"nomcli\": \"ANDA-ANDA, S.A.\",\n \"codrep\": \" 2\",\n \"telcli\": \"93.234.23.23\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 18\",\n \"nomcli\": \"SA VELO, S.A.\",\n \"codrep\": \" 3\",\n \"telcli\": \"902.222.333\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 6\",\n \"nomcli\": \"VIATHLON, S.L.\",\n \"codrep\": \" 1\",\n \"telcli\": \"902.555.231\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 7\",\n \"nomcli\": \"DEPOR-CAMPO, S.L.\",\n \"codrep\": \" 4\",\n \"telcli\": \"902.411.113\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 8\",\n \"nomcli\": \"NEU BLANCA, S.A.\",\n \"codrep\": \" 3\",\n \"telcli\": \"93.414.13.12\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 9\",\n \"nomcli\": \"EUROFRED\",\n \"codrep\": \" 2\",\n \"telcli\": \"902.412.323\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 10\",\n \"nomcli\": \"SKI, S.L.\",\n \"codrep\": \" 1\",\n \"telcli\": \"902.567.111\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 11\",\n \"nomcli\": \"NIEVE BLANCA, S.A.\",\n \"codrep\": \" 4\",\n \"telcli\": \"902.567.213\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 12\",\n \"nomcli\": \"REPROSPORT\",\n \"codrep\": \" 3\",\n \"telcli\": \"902.137.420\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 13\",\n \"nomcli\": \"MG-DEPORTES\",\n \"codrep\": \" 2\",\n \"telcli\": \"924.11.11.11\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 14\",\n \"nomcli\": \"DEPORTES LOPEZ, S.L.\",\n \"codrep\": \" 2\",\n \"telcli\": \"95.234.31.24\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 15\",\n \"nomcli\": \"SPORTS MARÍA\",\n \"codrep\": \" 4\",\n \"telcli\": \"91.567.65.43\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \"CONTADO\",\n \"nomcli\": \"Clientes contado\",\n \"codrep\": null,\n \"telcli\": \"\",\n \"e_mail\": \"\"\n },\n {\n \"codcli\": \" 16\",\n \"nomcli\": \"DEPORTES DE ARAGÓN, S.L.\",\n \"codrep\": \" 2\",\n \"telcli\": \"900.456.7.89\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 17\",\n \"nomcli\": \"BICICLETA ASTURIANA, S.L.\",\n \"codrep\": \" 4\",\n \"telcli\": \"900.222.113\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 5\",\n \"nomcli\": \"DEPORTES CANTABRIA\",\n \"codrep\": \" 3\",\n \"telcli\": \"902.222.333\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 19\",\n \"nomcli\": \"DEPORTES PALMERA, S.A.\",\n \"codrep\": \" 2\",\n \"telcli\": \"902.333.444\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 20\",\n \"nomcli\": \"DEPORTES DE RUEDAS, S.L.\",\n \"codrep\": \" 4\",\n \"telcli\": \"902.123.433\",\n \"e_mail\": \"[email protected]\"\n },\n {\n \"codcli\": \" 21\",\n \"nomcli\": \"BICIS LA REAL, S.L.\",\n \"codrep\": \" 3\",\n \"telcli\": \"902.400.187\",\n \"e_mail\": \"[email protected]\"\n }\n]"
Me sale este error cuando compilo
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'JsonA3ERP.json' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.'
¿Cómo lo puedo hacer?
jsonString
? El error está claro, el texto que le pasas no tiene estructura json