我想解决这个问题,
我想使用 ArraySegment 传输 Json 对象
我的 JSON 数据是 以这种格式
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
我用过this创建指定类的工具,如
public class Employee
{
public string firstName { get; set; }
public string lastName { get; set; }
}
public class RootObject
{
public List<Employee> employees { get; set; }
}
现在我像这样将数据插入到对象中
var objectToSerialize = new RootObject();
objectToSerialize.items = new List<Employee>
{
new Item { firstName="abc", lastName="xyz"},
new Item { firstName="pqr", lastName="stu"}
};
但现在我遇到了问题,我不知道如何使用 网络套接字
通过这个
ArraySegment<byte> max = new ArraySegment<byte>(, , );
await webSocket.SendAsync(max, WebSocketMessageType.Text, true, CancellationToken.None);
最佳答案
您需要先将objectToSerialize 序列化为一个字节缓冲区
var data = JsonConvert.SerializeObject(objectToSerialize);
var encoded = Encoding.UTF8.GetBytes(data);
var buffer = new ArraySegment<Byte>(encoded, 0, encoded.Length);
await webSocket.SendAsync.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
关于c# - 使用 ArraySegment 类到 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35321164/