在 Unity 中向 Notion API 发出 POST 请求

时间:2021-06-21 20:50:08

标签: c# api unity3d notion-api

我正在尝试在 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 格式,我现在的方式取得了一些进展,但表示父级未定义。

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();

这就是我得到的错误,这不是很有帮助。 enter image description here

需要任何帮助。

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

1 个答案:

答案 0 :(得分:2)

Unity Serializer 不支持属性

见 (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;
    }
}

<块引用>

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

当然你可以

它甚至是一个软件包:NewtonSoft JSON 您可以通过 Package Manager 简单地安装

enter image description here

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