从另一个程序接收 JSON 编码信息到我的 GUI
Receiving JSON encoded information from another program to my GUI
我需要从另一个程序接收 JSON 信息并将其显示在我的 GUI 中。我的当前代码刚刚使程序崩溃。我已经研究了大约一个星期的问题,但似乎找不到解决方案。
这段代码应该从程序接收信息并且应该反序列化数据。
public void TelemetryUpdateReceived()
{
TelemetryUpdate tUpdate;
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[256];
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
while (client != null)
{
int num_bytes = 0;
num_bytes = stream.Read(buffer, 0, 256);
string message = Encoding.ASCII.GetString(buffer, 0, num_bytes);
DisplayMessages(message);
tUpdate = javaScriptSerializer.Deserialize<TelemetryUpdate>(message);
data.Altitude = tUpdate.Altitude;
data.Elevator = tUpdate.ElevatorPitch;
data.Pitch = tUpdate.Pitch;
data.Speed = tUpdate.Speed;
data.Throttle = tUpdate.Throttle;
data.VerticalSpeed = tUpdate.VerticalSpeed;
data.WarningCode = tUpdate.WarningCode;
}
}
我还有一个class来获取和设置数据。
class getData
{
public double Altitude { get; set; }
public double Speed { get; set; }
public double Pitch { get; set; }
public double VerticalSpeed { get; set; }
public double Throttle { get; set; }
public double Elevator { get; set; }
public int WarningCode { get; set; }
}
struct TelemetryUpdate
{
public double VerticalSpeed;
public double Throttle;
public double Altitude;
public double Speed;
public double Pitch;
public double ElevatorPitch;
public int WarningCode;
}
编辑:我忘记添加错误消息屏幕截图 Error Message I Get
编辑 2:这是我用来显示数据的代码
private void DisplayMessages(string messageJ)
{
ListViewItem listViewItem = new ListViewItem(new string[] { messageJ });
this.lstBoxLatestData.Items.Insert(0, listViewItem);
if (this.lstBoxLatestData.Items.Count > 40)
{
lstBoxLatestData.Items.RemoveAt(lstBoxLatestData.Items.Count - 1);
}
}
设置断点后,这是 JSON 的示例,它引发了带有 "Invalid JSON primitive" 消息的异常:
{"Altitude":-1.2874016160000004,"Speed":0,"Pitch":0,"VerticalSpeed":0,"Throttle":0,"ElevatorPitch":0,"WarningCode":0}
{"Altitude":-1.2874016160000004,"Speed":0,"Pitch":0,"VerticalSpeed":0,"Throttle":0,"ElevatorPitch":0,"WarningCode":0}
那是无效的 JSON。它应该是:
- 单个元素
{
"Altitude":-1.2874016160000004,
"Speed":0,
"Pitch":0,
"VerticalSpeed":0,
"Throttle":0,
"ElevatorPitch":0,
"WarningCode":0
}
- 一组对象。
注意:在这种情况下,您还应该更改 C#
结构以存储数组,最近的 Visual Studio 版本确实有一个 "Paste JSON as classes" 功能,您可以使用
[
{
"Altitude":-1.2874016160000004,
"Speed":0,
"Pitch":0,
"VerticalSpeed":0,
"Throttle":0,
"ElevatorPitch":0,
"WarningCode":0
},
{
"Altitude":-1.2874016160000004,
"Speed":0,
"Pitch":0,
"VerticalSpeed":0,
"Throttle":0,
"ElevatorPitch":0,
"WarningCode":0
}
]
我需要从另一个程序接收 JSON 信息并将其显示在我的 GUI 中。我的当前代码刚刚使程序崩溃。我已经研究了大约一个星期的问题,但似乎找不到解决方案。
这段代码应该从程序接收信息并且应该反序列化数据。
public void TelemetryUpdateReceived()
{
TelemetryUpdate tUpdate;
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[256];
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
while (client != null)
{
int num_bytes = 0;
num_bytes = stream.Read(buffer, 0, 256);
string message = Encoding.ASCII.GetString(buffer, 0, num_bytes);
DisplayMessages(message);
tUpdate = javaScriptSerializer.Deserialize<TelemetryUpdate>(message);
data.Altitude = tUpdate.Altitude;
data.Elevator = tUpdate.ElevatorPitch;
data.Pitch = tUpdate.Pitch;
data.Speed = tUpdate.Speed;
data.Throttle = tUpdate.Throttle;
data.VerticalSpeed = tUpdate.VerticalSpeed;
data.WarningCode = tUpdate.WarningCode;
}
}
我还有一个class来获取和设置数据。
class getData
{
public double Altitude { get; set; }
public double Speed { get; set; }
public double Pitch { get; set; }
public double VerticalSpeed { get; set; }
public double Throttle { get; set; }
public double Elevator { get; set; }
public int WarningCode { get; set; }
}
struct TelemetryUpdate
{
public double VerticalSpeed;
public double Throttle;
public double Altitude;
public double Speed;
public double Pitch;
public double ElevatorPitch;
public int WarningCode;
}
编辑:我忘记添加错误消息屏幕截图 Error Message I Get
编辑 2:这是我用来显示数据的代码
private void DisplayMessages(string messageJ)
{
ListViewItem listViewItem = new ListViewItem(new string[] { messageJ });
this.lstBoxLatestData.Items.Insert(0, listViewItem);
if (this.lstBoxLatestData.Items.Count > 40)
{
lstBoxLatestData.Items.RemoveAt(lstBoxLatestData.Items.Count - 1);
}
}
设置断点后,这是 JSON 的示例,它引发了带有 "Invalid JSON primitive" 消息的异常:
{"Altitude":-1.2874016160000004,"Speed":0,"Pitch":0,"VerticalSpeed":0,"Throttle":0,"ElevatorPitch":0,"WarningCode":0}
{"Altitude":-1.2874016160000004,"Speed":0,"Pitch":0,"VerticalSpeed":0,"Throttle":0,"ElevatorPitch":0,"WarningCode":0}
那是无效的 JSON。它应该是:
- 单个元素
{
"Altitude":-1.2874016160000004,
"Speed":0,
"Pitch":0,
"VerticalSpeed":0,
"Throttle":0,
"ElevatorPitch":0,
"WarningCode":0
}
- 一组对象。
注意:在这种情况下,您还应该更改 C#
结构以存储数组,最近的 Visual Studio 版本确实有一个 "Paste JSON as classes" 功能,您可以使用
[
{
"Altitude":-1.2874016160000004,
"Speed":0,
"Pitch":0,
"VerticalSpeed":0,
"Throttle":0,
"ElevatorPitch":0,
"WarningCode":0
},
{
"Altitude":-1.2874016160000004,
"Speed":0,
"Pitch":0,
"VerticalSpeed":0,
"Throttle":0,
"ElevatorPitch":0,
"WarningCode":0
}
]