您好,我想解析从 websocket 服务器获得的数据,但它显示“无法从 'string' 转换为 'int'
当我这样做时,它给了我一个错误的数字示例:
- 实际价值:-11.6666342423411
- 它说的值:-1.166663424234E + 16
void Update()
{
ws.OnMessage += (sender, e) =>
{
JSONNode data = JSON.Parse(e.Data);
Debug.Log(data);
// position.x = int.Parse(e.Data["position"]["x"]);
// position.y = int.Parse(e.Data["position"]["y"]);
// Debug.Log(position);
// gameObject.transform.position = position;
// float rotation = data["rotation"];
// rb.rotation = rotation;
};
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WebSocketSharp;
using SimpleJSON;
public class Enemy : MonoBehaviour
{
public Rigidbody2D rb;
Vector2 position;
WebSocket ws;
void Start()
{
ws = new WebSocket("ws://localhost:8080");
ws.Connect();
}
void Update()
{
ws.OnMessage += (sender, e) =>
{
// JSONNode data = JSON.Parse(e.Data);
Debug.Log(e.Data["position"]);
// position.x = int.Parse(e.Data["position"]["x"]);
// position.y = int.Parse(e.Data["position"]["y"]);
// Debug.Log(position);
// gameObject.transform.position = position;
// float rotation = data["rotation"];
// rb.rotation = rotation;
};
}
}