我正在从控制台应用程序中使用 WebService。我遇到的问题是当我收到 Json 并尝试反序列化它时,它返回错误:“它不支持反序列化数组”。
Json结构如下:
[{
"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}"
}]
代码如下:
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;
}
}
}
}
如果有人可以帮助我,非常感谢。
首先将类的所有字符传递给字符串
Registro
PS:如果你有转换问题,那么你应该
id y serial1
去string
现在要将 json 反序列化为一个对象,您有两个选择:
选项 1:当你去反序列化 json 时,首先你得到列表,
Registro
然后将它们分配给你的类Encabezado
选项 2:json 具有以下格式:
这样,您只需在执行操作时应用反序列化: