c# - 在 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。
解决方案
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 版本中
推荐阅读
- javascript - ReactJS:来自 Express 的数据显示为数字而不是 JSON
- c++ - 标准差公式
- python - 安装 Tensorflow 时遇到问题 - 为 wrapt 构建轮子(setup.py)...错误
- python - django-admin.py startproject mysite 不工作
- intellij-idea - IdeaVim:从命令更改为插入模式并返回等时,有没有办法更改背景颜色?例如每种模式都有自己的背景颜色
- java - 由于只读消息和 Mac 上的灰色编辑器,我无法在 NetBeans 中编辑文件
- swift - 如何在 SwiftUI 中删除主要内容视图和标签栏之间的间距?
- graphql - NestJS/graphql - 拦截 Apollo HTTP 错误
- postgresql - 无法将主机名“postgres”转换为地址:名称或服务未知
- python - 如何在机器学习中使用 cross_val_score 进行预测