首页 > 解决方案 > 在 Unity 中向 Notion API 发出 POST 请求

问题描述

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

    [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 格式,我现在的方式取得了一些进展,但说 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。

标签: c#apiunity3dnotion-api

解决方案


Unity Serializer 不支持属性

请参阅(脚本序列化)。

只需删除所有{get; set; }so 而不是属性,您将拥有字段

[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;
    }
}

因为它是统一的,所以我不能使用 Newtonsoft.Json,(否则这将是非常简单的任务)

当然可以

它甚至是一个包:NewtonSoft JSON,您可以通过包管理器简单地安装

在此处输入图像描述

afaik 它甚至预装在最新的 Unity 版本中


推荐阅读