0

我在一个天蓝色的函数应用程序中序列化一个类,并通过对 Unity3d 中的帖子的响应来收集它。这是一个 WebGL 项目,它在编辑器上运行良好,但在浏览器(chrome/firefox)上抛出异常:

using Newtonsoft.Json;
    namespace ARTFunctions
    {
    public class LoginResponse
        {
            public string status { get; set; }
            public string token { get; set; }
            public string clientDB { get; set; }
            public string clientTable { get; set; }
            [JsonConstructor]
            public LoginResponse() { }
        }
    public async Task<System.String> MyFunctionApp([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "head", Route = null)] HttpRequest req, ILogger log)
            {
    LoginResponse logResp = new LoginResponse();
    logResp.status = "Success";
    logResp.clientDB = "userDB";
    logResp.clientTable = "thisTable";
    logResp.token="abc";
    return JsonConvert.SerializeObject(logResp);
    }

在 Unity3d 中,我收集序列化对象但得到构造函数错误:

using Newtonsoft.Json;
public class C_UnityClass: MonoBehaviour
{
    public void AtButtonPressed()
        {
          UnityWebRequest www = UnityWebRequest.Post("https://xxx.azurewebsites.net/api/MyFunctionApp", formData);
          yield return www.SendWebRequest();
          string queryResult = www.downloadHandler.text;
          var logResp = JsonConvert.DeserializeObject<LoginResponse>(queryResult);
          debug.log("logResp"+logResp.token);
        }
    }

并使用 JsonContructor 创建非 Mono LoginResponse 类:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class LoginResponse
{
   public string status { get; set; }
   public string token { get; set; }
   public string clientDB { get; set; }
   public string clientTable { get; set; }
   [JsonConstructor]
   public LoginResponse (){}
   //I also tried adding [JsonConstructor] to this constructor but throws error too
   public LoginResponse(string status, string token, string clientDB, string clientTable)
 {
    this.status = status;
    this.token = token;
    this.clientDB = clientDB;
    this.clientTable = clientTable;
 }
}

错误:JsonSerializationException:找不到用于类型 LoginResponse 的构造函数。一个类应该有一个默认构造函数、一个带参数的构造函数或一个标有 JsonConstructor 属性的构造函数。路径“状态”,第 1 行,位置 10。在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(Newtonsoft.Json.JsonReader 阅读器,Newtonsoft.Json.Serialization.JsonObjectContract objectContract,Newtonsoft.Json.Serialization.JsonProperty containerMember,Newtonsoft.Json .Serialization.JsonProperty containerProperty, System.String id, System.Boolean& createdFromNonDefaultCreator) [0x00000] in <000000000000000000000000000000000>:0

编辑:如果我对在 [JsonConstructor] 标记的 LoginResponse 使用单个空构造函数,我会得到异常,就好像构造函数在 azure function app 处失败一样。值得注意的是,当从 Unity 编辑器运行时,这一切都有效,在浏览器上失败(针对 chrome/firefox 测试):

"Unexpected end of Stream, the content may have already been read by another component. ","token":null,"clientDB":null,"clientTable":null}"

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class LoginResponse
{
   public string status { get; set; }
   public string token { get; set; }
   public string clientDB { get; set; }
   public string clientTable { get; set; }
   [JsonConstructor]
   public LoginResponse (){}
}
4

0 回答 0