在 Unity 中向 Notion API 发出 POST 请求

making POST request to Notion API in Unity

我正在尝试在 Unity 中向 Notion API 发出 POST 请求。我有一个 class,其中包含我根据概念要求创建的所有属性。

    [Serializable]
    public class Parent
    {
        public string Database_id { get; set; }
        public Parent(string database_id)
        {
            Database_id = database_id;
        }
    }

    [Serializable]
    public class Text
    {
        public string Content { get; set; }

        public Text(string content)
        {
            Content = content;
        }
        //public List<RichText> rich_text { get; set; }
    }

    [Serializable]
    public class Title
    {
        public Text Text { get; set; }
        public Title(Text text)
        {
            Text = text;
        }
    }

    [Serializable]
    public class Name
    {
        public List<Title> title { get; set; }
        public Name(List<Title> titles)
        {
            title = titles;
        }
    }

    [Serializable]
    public class Properties
    {
        public Name Name { get; set; }

        public Properties(Name name)
        {
            Name = name;
        }
    }

    [Serializable]
    public class Root
    {
        public Parent Parent { get; set; }
        public Properties Properties { get; set; }

        public Root(Parent parent, Properties properties)
        {
            parent = parent;
            properties = properties;
        }
    }

这就是我调用它的方式,我尝试将 json 字符串转换为字节,但我收到错误消息,它是错误的 json 格式,而我现在的方式使一些progress 但说 parent 是未定义的。

var url = $"https://api.notion.com/v1/pages";
        var parent = new Parent(databaseId);
        var txt = new Text("test");
        var title = new Title(txt);
        var nam = new Name(new List<Title>() { title });
        var prop = new Properties(nam);
        var root = new Root(parent, prop);


        string json = JsonUtility.ToJson(root);

        UnityWebRequest www = new UnityWebRequest(url, "POST");
        byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
        www.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
        www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();

        www.SetRequestHeader("Authorization", userSecret);
        www.SetRequestHeader("notion_version", Static.NOTION_VER);
        www.SetRequestHeader("Content-Type", "application/json");

        yield return www.SendWebRequest();

这就是我得到的错误,它不是很有用。

欢迎提供任何帮助。

编辑: 我已经删除了 { get;放; } 像 derHugo 建议的那样,但是我还需要用小写字母制作一些字段,例如。 Database_id 到 database_id。

Unity 序列化程序不支持 属性

参见 (Script Serialzation)。

只需删除所有 {get; set; },这样您将拥有 字段

而不是属性
[Serializable]
public class Parent
{
    public string Database_id;

    public Parent(string database_id)
    {
        Database_id = database_id;
    }
}

[Serializable]
public class Text
{
    public string Content;

    public Text(string content)
    {
        Content = content;
    }
}

[Serializable]
public class Title
{
    public Text Text;

    public Title(Text text)
    {
        Text = text;
    }
}

[Serializable]
public class Name
{
    public List<Title> title;

    public Name(List<Title> titles)
    {
        title = titles;
    }
}

[Serializable]
public class Properties
{
    public Name Name;

    public Properties(Name name)
    {
        Name = name;
    }
}

[Serializable]
public class Root
{
    public Parent Parent;
    public Properties Properties;

    public Root(Parent parent, Properties properties)
    {
        parent = parent;
        properties = properties;
    }
}

because it's unity I can't use Newtonsoft.Json, (otherwise it would be very simple task)

当然你可以

甚至是一个包裹:NewtonSoft JSON you can simply install via the Package Manager

据我所知,它甚至预装在最新的 Unity 版本中