无法将 JSON 转换为 C# class
Can't convert JSON to C# class
我在转换 JSON 响应 C# class 时遇到了一些麻烦。
我将以下 JSON 回复到字符串对象中:
"[{\"slaveId\":31,\"funcCode\":3,\"address\":86,\"quantity\":2,\"data\":[4,30,241,73,0]}]"
我在 this web site 上验证了格式,字符串是有效的 JSON 格式。
我使用 json2csharp 将上述响应转换为 C# class 并得到以下代码:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class MyArray
{
public int slaveId { get; set; }
public int funcCode { get; set; }
public int address { get; set; }
public int quantity { get; set; }
public List<int> data { get; set; }
}
public class Root
{
public List<MyArray> MyArray { get; set; }
}
我把上面的代码添加到我的项目中,最后的结果是:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
namespace ModbusJsonTest
{
class Program
{
static void Main(string[] args)
{
const int PORT_NO = 30001;
const string SERVER_IP = "192.168.1.1";
//---data to send to the server---
StringBuilder request = new();
request.Append("{");
request.Append("\"funcCode\":3,");
request.Append("\"slaveId\":31,");
request.Append("\"address\":86,");
request.Append("\"quantity\":2,");
request.Append("\"interval\":0");
request.Append("}");
//---create a TCPClient object at the IP and port no.---
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
NetworkStream nwStream = client.GetStream();
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(request.ToString());
//---send the text---
Console.WriteLine("Sending : " + request.ToString() + "/n");
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
//---read back the text---
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
string myJsonResponse = Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
client.Close();
}
}
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class MyArray
{
public int slaveId { get; set; }
public int funcCode { get; set; }
public int address { get; set; }
public int quantity { get; set; }
public List<int> data { get; set; }
}
public class Root
{
public List<MyArray> MyArray { get; set; }
}
不幸的是,此行的代码中断(在//---读回文本---部分):
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
我收到以下错误:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ModbusJsonTest.Root' 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.'
我尝试使用 JSON 格式字符串,删除空格,使用 "name":value" 格式
取而代之的是“名称”:值格式等...也没有用。
我不知道我做错了什么?
我觉得我在这里做了所有的事情...
任何代码examples/suggestions我可以试试吗?
class Root
的定义仅用于数组反序列化目的。然后我们可以利用 Json.NET 功能。
Json.NET 支持 List
、Array
、Collection
的内置列表反序列化
var myDeserializedClass = JsonConvert.DeserializeObject<List<MyArray>>();
我在转换 JSON 响应 C# class 时遇到了一些麻烦。
我将以下 JSON 回复到字符串对象中:
"[{\"slaveId\":31,\"funcCode\":3,\"address\":86,\"quantity\":2,\"data\":[4,30,241,73,0]}]"
我在 this web site 上验证了格式,字符串是有效的 JSON 格式。
我使用 json2csharp 将上述响应转换为 C# class 并得到以下代码:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class MyArray
{
public int slaveId { get; set; }
public int funcCode { get; set; }
public int address { get; set; }
public int quantity { get; set; }
public List<int> data { get; set; }
}
public class Root
{
public List<MyArray> MyArray { get; set; }
}
我把上面的代码添加到我的项目中,最后的结果是:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
namespace ModbusJsonTest
{
class Program
{
static void Main(string[] args)
{
const int PORT_NO = 30001;
const string SERVER_IP = "192.168.1.1";
//---data to send to the server---
StringBuilder request = new();
request.Append("{");
request.Append("\"funcCode\":3,");
request.Append("\"slaveId\":31,");
request.Append("\"address\":86,");
request.Append("\"quantity\":2,");
request.Append("\"interval\":0");
request.Append("}");
//---create a TCPClient object at the IP and port no.---
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
NetworkStream nwStream = client.GetStream();
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(request.ToString());
//---send the text---
Console.WriteLine("Sending : " + request.ToString() + "/n");
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
//---read back the text---
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
string myJsonResponse = Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
client.Close();
}
}
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class MyArray
{
public int slaveId { get; set; }
public int funcCode { get; set; }
public int address { get; set; }
public int quantity { get; set; }
public List<int> data { get; set; }
}
public class Root
{
public List<MyArray> MyArray { get; set; }
}
不幸的是,此行的代码中断(在//---读回文本---部分):
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
我收到以下错误:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ModbusJsonTest.Root' 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.'
我尝试使用 JSON 格式字符串,删除空格,使用 "name":value" 格式 取而代之的是“名称”:值格式等...也没有用。
我不知道我做错了什么?
我觉得我在这里做了所有的事情...
任何代码examples/suggestions我可以试试吗?
class Root
的定义仅用于数组反序列化目的。然后我们可以利用 Json.NET 功能。
Json.NET 支持 List
、Array
、Collection
var myDeserializedClass = JsonConvert.DeserializeObject<List<MyArray>>();