JsonSerializationException:无法找到 Unity webgl 构建的构造函数

问题描述 投票:0回答:2

我在 azure 函数应用程序中序列化一个类,并通过对 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 容器成员、Newtonsoft.Json.Serialization.JsonProperty 容器属性、System.String id、 System.Boolean&createdFromNonDefaultCreator) [0x00000] in <00000000000000000000000000000000>:0

编辑:如果我对标记为 [JsonConstructor] 的 LoginResponse 使用单个空构造函数,我会得到异常,就好像构造函数在 azure 函数应用程序中失败一样。值得注意的是,从 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 (){}
}
unity-game-engine unity-webgl jsonserializer azure-function-app unitywebrequest
2个回答
3
投票

请将您的条带引擎代码级别降低到低。

有效


0
投票

对我有用的是添加一个带有 [Preserve] 属性的空构造函数。从这里得到了解决方案。

示例:

[Preserve]
public ExampleConstructor() {}
© www.soinside.com 2019 - 2024. All rights reserved.