I am consuming a WebService from a console application. The problem I have is when I receive the Json and try to deserialize it, it returns the error: "it is not supported for deserializing an array".
The Json structure is as follows:
[{
"id": "19454",
"serial1": "4634170894",
"fecha1": "12-03-2018 00:19",
"sis_datetime": "2018-10-26 17:17:06",
"mileage": "None",
"voltage": "14.804",
"engine_operation": "1.0",
"lat": "-90.9160305",
"lon": "14.2409938",
"sis_packat_zike": "820",
"params": "{'custom1': 123.36666666661733, 'posinfo': {'c': 187, 'h': 132.46, 'lon': 14.2409938, 'sc': 10, 's': 0, 'lat': -90.9160305}, 'voltage': 14.804, 'report_type': 2, 'engine operation': 1.0, 'custom': 187.0, 'accum_9': 158, 'accum_8': 0, 'accum_3': 0, 'accum_2': 12251, 'accum_1': 4178, 'accum_0': 14804, 'accum_7': 0, 'accum_6': 0, 'accum_5': 0, 'accum_4': 0, 'event_index': 13, 'engine rpm': 1625.000000001248, 'event_code': 1, 'hdop': 1.2, 'unit_status': 0, 'host': '', 'accum_14': 0, 'seq_num': 42885, 'iridium': 0, 'rssi': -93, 'accum_15': 0, 'avl_inputs': 63, 'accum_13': 158, 'accum_12': 0, 'accum_11': 0, 'accum_10': 0}"
}]
The code is the following:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace WsMotobombas
{
class Program
{
static void Main(string[] args)
{
string myConnectionString = "";
var url ="";
using (var response = webrequest.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream()))
{
var result = reader.ReadToEnd();
string jsonstring = result;
var serializer = new JavaScriptSerializer();
//Encabezado FldEncabezado = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Encabezado>(jsonstring);
Encabezado FldEncabezado = serializer.Deserialize<Encabezado>(jsonstring);
using (SqlConnection conn = new SqlConnection(myConnectionString))
{
conn.Open();
foreach (var Registro in FldEncabezado.Items)
{
if (InsertData(conn, Registro))
{
Console.WriteLine(Registro.serial1 + " insertado Correctamente");
}
else
{
Console.WriteLine("Error : " + Registro.serial1 + " Registr no insertado");
}
}
}
Console.Read();
}
}
public class Registro
{
public int id { get; set; }
public int serial1 { get; set; }
public DateTime fecha1 { get; set; }
public DateTime sis_datetime { get; set; }
public char mileage { get; set; }
public char voltage { get; set; }
public char engine_operation { get; set; }
public char lat { get; set; }
public char lon { get; set; }
public char sis_packat_zike { get; set; }
public char paramsi { get; set; }
}
public class Encabezado
{
public List<Registro> Items { get; set; }
}
static bool InsertData(SqlConnection conn, Registro Lectura)
{
try
{
string xSql = @"INSERT INTO dbo.TblLecturas( Serial1 ,fecha1 ,sis_datetime ,mileage ,voltage ,engine_operation ,lat ,lon ,sis_packat_zike)";
using (SqlCommand cmd = new SqlCommand(xSql, conn))
{
cmd.Parameters.Add(new SqlParameter("@Serial1", Lectura.serial1));
//cmd.Parameters.Add(new SqlParameter("@fecha1", Lectura.fecha1));
//cmd.Parameters.Add(new SqlParameter("@sis_datetime", Lectura.sis_datetime));
//cmd.Parameters.Add(new SqlParameter("@mileage", Lectura.mileage));
//cmd.Parameters.Add(new SqlParameter("@voltage", Lectura.voltage));
//cmd.Parameters.Add(new SqlParameter("@engine_operation", Lectura.engine_operation));
//cmd.Parameters.Add(new SqlParameter("@lat", Lectura.lat));
//cmd.Parameters.Add(new SqlParameter("@lon", Lectura.lon));
//cmd.Parameters.Add(new SqlParameter("@sis_packat_zike", Lectura.sis_packat_zike));
cmd.ExecuteNonQuery();
}
return true;
}
catch (Exception e)
{
return false;
}
}
}
}
If anyone can help me, thank you very much.
First pass all the chars of the class to string
Registro
PS: If you have a conversion problem then you should go
id y serial1
to astring
Now to deserialize the json to an object you have two options:
Option 1 : When you go to deserialize the json, first you get the list of
Registro
and then you assign them to your classEncabezado
Option 2 : That the json has the following format:
This way you only apply the deserialization as you are doing: