【发布时间】:2021-09-07 16:32:04
【问题描述】:
我在将 JSON 响应转换为 C# 类时遇到了一些麻烦。
我将以下 JSON 响应转换为字符串对象:
"[{\"slaveId\":31,\"funcCode\":3,\"address\":86,\"quantity\":2,\"data\":[4,30,241,73,0]}]"
我在 this web site 上验证了格式,该字符串是有效的 JSON 格式。
我使用 json2csharp 将上述响应转换为 C# 类并得到以下代码:
// 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; }
}
不幸的是,这一行的代码中断(在 //---read back the text--- 部分):
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
我收到以下错误:
Newtonsoft.Json.JsonSerializationException: '无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'ModbusJsonTest.Root',因为该类型需要 JSON 对象(例如 {"name":"value" }) 正确反序列化。 要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),例如可以从 JSON 数组反序列化。 JsonArrayAttribute 也可以添加到类型中以强制它从 JSON 数组反序列化。 路径'',第 1 行,位置 1。'
我尝试使用 JSON 格式字符串,删除空格,使用 "name":value" 格式 而是“名称”:值格式等...也不起作用。
我不知道我做错了什么?
我觉得我在这里按部就班地做了一切......
我可以尝试任何代码示例/建议吗?
【问题讨论】:
-
JSON 中的根是数组,因此 C# 中的根应该是集合类型(列表、可枚举、数组等)。就这么简单。
-
你只需要 JsonConvert.DeserializeObject
- >(myJsonResponse) 代替。
-
如果
Root只是为了反序列化而创建的,你应该忽略它。并反序列化 MyArray 项目列表。var myDeserializedClass = JsonConvert.DeserializeObject<List<MyArray>>(myJsonResponse); -
如果您使用的是 Newtonsoft,那么您可以将其作为
JArray进行操作。 -
@LeVu 请添加您的建议作为答案,它有效,我会接受它,谢谢!