从Python服务器发送JSON
使用PyZMQ在Unity中设置客户端套接字连接。 服务器使用一个JSON对象进行响应,该对象在使用Python JSON.dumps()方法发送之前转换为字符串 我的问题是,在将JSON对象发送到客户端之前,它应该转换为字符串还是作为JSON对象 现在我收到从Python服务器发送JSON,python,c#,json,sockets,Python,C#,Json,Sockets,使用PyZMQ在Unity中设置客户端套接字连接。 服务器使用一个JSON对象进行响应,该对象在使用Python JSON.dumps()方法发送之前转换为字符串 我的问题是,在将JSON对象发送到客户端之前,它应该转换为字符串还是作为JSON对象 现在我收到 "{\"timestamp\": 1233456,\"foo\": 2,\"nofoo\": 0,\"foo_count\": 234,\"
"{\"timestamp\": 1233456,\"foo\": 2,\"nofoo\": 0,\"foo_count\": 234,\"nofoo_count\": 12}"
这使得解析变得很困难。您已经添加了C#标记,因此在C#(dotnet core 3.1)中解析此消息应该非常简单
using System.Text.Json.Serialization;
var data = "{\"timestamp\": 1233456,\"foo\": 2,\"nofoo\": 0,\"foo_count\": 234,\"nofoo_count\": 12}";
var jsonData = JsonSerializer.Deserialize<MyData>(data);
看起来你已经回答了自己的问题——如果你先转换成字符串,然后再转换成JSON,那就是你得到的。JSON就是一个字符串。您也可以说是“JSON字符串”。这就是您从
json.dumps()
获得的信息。当您收到它时,必须使用json.loads()
将其转换为原始对象为什么json很难解析?您应该能够使用JSON反序列化。
class MyData
{
public int timestamp { get; set; }
public int foo { get; set; }
public int nofoo { get; set; }
public int foo_count { get; set; }
public int nofoo_count { get; set; }
}