从字典中正确地将字符串转换为 Json
Converting String to Json Properly from the Dictionary
我想向 Flask 服务器发送 json 请求。我使用 UnityWebRequest
来执行以下操作,
IEnumerator Post(string url, string bodyJsonString)
{
var request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
}
我使用下面的代码来创建 json,
Dictionary<string, string> RootJson = new Dictionary<string, string>();
Dictionary<string, float> id_val = new Dictionary<string, float>();
Dictionary<string,byte[]> employeeData = new Dictionary<string, byte[]>();
CalibrationJson.Add("id",1);
imagesJson.Add("employeeData",encodedByte);
RootJson.Add("id", JsonConvert.SerializeObject(id_val.ToString()));
RootJson.Add("data", JsonConvert.SerializeObject(employeeData.ToString()));
StartCoroutine(Post("http://127.0.0.1:5000/emp_detail", JsonConvert.SerializeObject(RootJson.ToString())));
然而它并没有创建一个合适的 json 虽然我每次都序列化了字典。它在服务器中抛出以下错误,
System.Collections.Generic.Dictionary`2[System.String,System.String]
我错过了什么?
从您的序列化代码中删除 ToString
:
RootJson.Add("id", JsonConvert.SerializeObject(id_val));
RootJson.Add("data", JsonConvert.SerializeObject(employeeData));
StartCoroutine(Post("http://127.0.0.1:5000/emp_detail", JsonConvert.SerializeObject(RootJson)));
对于不覆盖 ToString
的类型,它将 return 类型名称,因此对于 Dictionary<string, string>()
你将得到 "System.Collections.Generic.Dictionary`2 [System.String,System.String]":
Console.WriteLine(new Dictionary<string, string>().ToString()); // prints System.Collections.Generic.Dictionary`2[System.String,System.String]
我想向 Flask 服务器发送 json 请求。我使用 UnityWebRequest
来执行以下操作,
IEnumerator Post(string url, string bodyJsonString)
{
var request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
}
我使用下面的代码来创建 json,
Dictionary<string, string> RootJson = new Dictionary<string, string>();
Dictionary<string, float> id_val = new Dictionary<string, float>();
Dictionary<string,byte[]> employeeData = new Dictionary<string, byte[]>();
CalibrationJson.Add("id",1);
imagesJson.Add("employeeData",encodedByte);
RootJson.Add("id", JsonConvert.SerializeObject(id_val.ToString()));
RootJson.Add("data", JsonConvert.SerializeObject(employeeData.ToString()));
StartCoroutine(Post("http://127.0.0.1:5000/emp_detail", JsonConvert.SerializeObject(RootJson.ToString())));
然而它并没有创建一个合适的 json 虽然我每次都序列化了字典。它在服务器中抛出以下错误,
System.Collections.Generic.Dictionary`2[System.String,System.String]
我错过了什么?
从您的序列化代码中删除 ToString
:
RootJson.Add("id", JsonConvert.SerializeObject(id_val));
RootJson.Add("data", JsonConvert.SerializeObject(employeeData));
StartCoroutine(Post("http://127.0.0.1:5000/emp_detail", JsonConvert.SerializeObject(RootJson)));
对于不覆盖 ToString
的类型,它将 return 类型名称,因此对于 Dictionary<string, string>()
你将得到 "System.Collections.Generic.Dictionary`2 [System.String,System.String]":
Console.WriteLine(new Dictionary<string, string>().ToString()); // prints System.Collections.Generic.Dictionary`2[System.String,System.String]