Typescript -如何从Sanity API结果中提取值- JSON

goucqfw6  于 2023-08-08  发布在  TypeScript
关注(0)|答案(1)|浏览(105)

我试图从Sanity Client API调用中获取值,但不知道如何从结果JSON中获取值。
下面是API调用的结果:

[
  {
    slug: { current: "test-post", _type: "slug" },
    authors: [
      {
        last_name: "xxx",
        profile_picture: "/images/xxx.png",
        _key: "xx",
        first_name: "xx",
        email: "xxx",
        _type: "author",
        bio: "xxxx  ",
      },
    ],
    _id: "xxx",
    banner_image: {
      alt_text: "Test Caption",
      _type: "image",
      caption: "Test Caption",
    },
    category: ["Strategy"],
    _updatedAt: "2023-02-18T15:43:28Z",
    headline:
      "Lets make a big deal of this post.  We want to bring in traffic to this post, so lets make it happen",
    publish_date: "2023-01-21T23:34:00.000Z",
    _rev: "0yPlmSh0SROjoBusCs2gsT",
    _type: "post",
    body: [
      {
        _key: "xxx",
        markDefs: [],
        children: [
          {
            text: "THIS IS A TEST POST",
            _key: "e256bcb6daf8",
            _type: "span",
            marks: ["strong"],
          },
        ],
        _type: "block",
        style: "h1",
      },
      {
        _key: "8ed793be8c68",
        markDefs: [],
        children: [
          { _type: "span", marks: [], text: "", _key: "2192a311988f" },
        ],
        _type: "block",
        style: "normal",
      },
      {
        style: "normal",
        _key: "ccbd634646c7",
        markDefs: [],
        children: [
          {
            _key: "ca3743dc66a2",
            _type: "span",
            marks: [],
            text: "THis is a test post ... THis is a test post ... THis is a test post ... a test post ... THis is a test post ... ",
          },
        ],
        _type: "block",
      },
      {
        markDefs: [],
        children: [
          { _type: "span", marks: [], text: "", _key: "bbd0babc70ff" },
        ],
        _type: "block",
        style: "normal",
        _key: "2a2b1d91ce0b",
      },
      {
        _type: "block",
        style: "h2",
        _key: "bf164f7a43db",
        markDefs: [],
        children: [
          {
            _type: "span",
            marks: ["strong"],
            text: "THIS IS A TEST POST",
            _key: "3544ed002a9c",
          },
        ],
      },
      {
        _key: "2cdb59177048",
        markDefs: [],
        children: [
          {
            _key: "4ef42b5af8c5",
            _type: "span",
            marks: [],
            text: "\na test post ... THis is a test post ... THis is a test post ... THis is a test post ... THis is a test post ... THis is a test post ... THis is a test post ... THis is a test post ... THis is a test post ... THis is a test post ... THis is a test post ... THis is a test post ... THis is a test post ... ",
          },
        ],
        _type: "block",
        style: "normal",
      },
      {
        _key: "14cc34e887dc",
        markDefs: [],
        children: [
          { marks: [], text: "", _key: "1fecc2c87c1c", _type: "span" },
        ],
        _type: "block",
        style: "normal",
      },
      {
        _type: "block",
        style: "normal",
        _key: "882f2ad2088d",
        markDefs: [],
        children: [
          { text: "", _key: "247eac8e2666", _type: "span", marks: [] },
        ],
      },
    ],
    title: "This is the title of the post2",
    _createdAt: "2023-01-21T23:38:11Z",
  },
];

字符串
下面是我尝试处理结果的代码。

const result = await SanityClient.fetch(`*[_type == "post"] | order(publish_date desc)`,);
        const document = JSON.stringify(result);
        const post:Post = {
            title: document['title'],
            slug: document['slug.current'],
            headline: document[1]['headline'],
            publish_date: document['publish_date'],
            banner_image: document['banner_image'],
            category: document['category'],
            author: document['author'],
            body: document['body']
        }


我不确定如何引用文档对象中的属性或键。比如如何上“头条”等
提前感谢您的洞察力!

5rgfhyps

5rgfhyps1#

我想出来了
我使用的是Stringify函数,它修改了Sanity结果的格式。我删除了声明

const document = JSON.stringify(result);

字符串
并通过使用result[0]['title']引用属性以获取标题...

相关问题