首页 > 解决方案 > WP_JSON 为移动应用准备数据,格式错误

问题描述

我正在尝试使用 wp-json rest api 从我的网站导出数据。应用程序应该以精确的格式读取这些数据,但我找不到提供正确结构的方法。

所需格式

事件应该按group类别分组,每个事件都应该有一个time参数,sessions我将存储该事件。这是应该的样子:

{
  "schedule": [
    {
      "date": "2047-05-17",
      "groups": [
        {
          "time": "26 July 2020",
          "sessions": [
            {
              "name": "Getting Started with Ionic",
              "location": "Hall 2",
              "description": "Mobile devices and browsers are now advanced enough that developers can build native-quality mobile apps using open web technologies like HTML5, Javascript, and CSS. In this talk, we’ll provide background on why and how we created Ionic, the design decisions made as we integrated Ionic with Angular, and the performance considerations for mobile platforms that our team had to overcome. We’ll also review new and upcoming Ionic features, and talk about the hidden powers and benefits of combining mobile app development and Angular.",
              "speakerNames": [
                "Ted Turtle"
              ],
              "timeStart": "9:30 am",
              "timeEnd": "9:45 am",
              "tracks": [
                "Concerti"
              ],
              "id": "2",
              "pic": "madison.jpg"
            },
            {
              "name": "Ionic Tooling",
              "location": "Executive Ballroom",
              "description": "Mobile devices and browsers are now advanced enough that developers can build native-quality mobile apps using open web technologies like HTML5, Javascript, and CSS. In this talk, we’ll provide background on why and how we created Ionic, the design decisions made as we integrated Ionic with Angular, and the performance considerations for mobile platforms that our team had to overcome. We’ll also review new and upcoming Ionic features, and talk about the hidden powers and benefits of combining mobile app development and Angular.",
              "speakerNames": [
                "Rachel Rabbit"
              ],
              "timeStart": "9:45 am",
              "timeEnd": "10:00 am",
              "tracks": [
                "Tooling"
              ],
              "id": "3",
              "pic": "chicago.jpg"
            },
            
          ]
        },
        {
          "time": "30 July 2020",
          "sessions": [
            {
              {
              "name": "University of Ionic",
              "location": "Hall 3",
              "description": "Mobile devices and browsers are now advanced enough that developers can build native-quality mobile apps using open web technologies like HTML5, Javascript, and CSS. In this talk, we’ll provide background on why and how we created Ionic, the design decisions made as we integrated Ionic with Angular, and the performance considerations for mobile platforms that our team had to overcome. We’ll also review new and upcoming Ionic features, and talk about the hidden powers and benefits of combining mobile app development and Angular.",
              "speakerNames": [
                "Ellie Elephant"
              ],
              "timeStart": "9:15 am",
              "timeEnd": "9:30 am",
              "tracks": [
                "Ionic"
              ],
              "id": "4",
              "pic": "seattle.jpg"
            }
            }
          ]
        }
      ]
    }
  ]
}

实际情况

sessions应该被包裹进groups,在当时,这些是不同的元素schedule

{
   "schedule":[
      {
         "date":"2020-08-28",
         "groups":[
            {
               "time":"28 Aug 2020",
               "sessions":[
                  {
                     "name":"Getting Started with Ionic",
                     "location":"Hall 2",
                     "description":"Mobile devices and browsers are now advanced enough that developers can build native-quality mobile apps using open web technologies like HTML5, Javascript, and CSS. In this talk, we’ll provide background on why and how we created Ionic, the design decisions made as we integrated Ionic with Angular, and the performance considerations for mobile platforms that our team had to overcome. We’ll also review new and upcoming Ionic features, and talk about the hidden powers and benefits of combining mobile app development and Angular.",
                     "speakerNames": [
                        "Rachel Rabbit"
                     ],
                     "timeStart": "9:45 am",
                     "timeEnd": "10:00 am",
                     "tracks": [
                        "Tooling"
                     ],
                     "id": "3",
                     "pic": "chicago.jpg"
                  }
               ]
            }
         ]
      },
      {
         "date":"2020-08-29",
         "groups":[
            {
               "time":"29 Aug 2020",
               "sessions":[
                  {
                     "name":"Andy e Norman – Teatro Lyrick",
                     "location":"Santa Maria degli Angeli",
                     "description":"Il duo comico Gigi e Ross debutta a teatro con la commedia Andy e Norman, fiaba moderna del commediografo newyorkese Neil Simon, messa in scena con grande successo a Broadway nel 1966. La vicenda ruota attorno a due scapoli che dividono lo stesso appartamento e, con grandi difficoltà, dirigono e producono una rivista alternativa. Ad alterare il già precario equilibrio tra i due ci penserà Sophie, attraente ragazza americana trasferitasi da poco nell'abitazione accanto.",
                     "timeStart":"29 Aug 2020 09:48",
                     "timeEnd":"29 Aug 2020 09:48",
                     "tracks":[
                        "Teatro"
                     ],
                     "id":5542,
                     "pic":"https://www.eventiassisi.it/wp-content/uploads/2020/02/225418127-61acfbf1-6fc4-45e0-b43c-1eb92d292776.webp"
                  }
               ]
            }
         ]
      },
      {
         "date":"2020-10-31",
         "groups":[
            {
               "time":"31 Oct 2020",
               "sessions":[
                  {
                     "name":"Pink Sonic – Assisi",
                     "location":"Santa Maria degli Angeli",
                     "description":"",
                     "timeStart":"31 Oct 2020 11:37",
                     "timeEnd":"31 Oct 2020 11:00",
                     "tracks":[
                        "Concerti"
                     ],
                     "id":5626,
                     "pic":"https://www.eventiassisi.it/wp-content/uploads/2020/07/pink-sonic.jpg"
                  }
               ]
            }
         ]
      }
   ]
}

实际代码

function app_get_newer_posts($data) {
    $args = array(
        'post_type' => 'event',
        'post_status' => 'publish',
        'posts_per_page' => 8,
        'paged' => $data['page']
    );
    $query = new WP_Query( $args );

    if ($query->have_posts()) {
        $ret = [];

        while ( $query->have_posts() ) {
            $term = get_term(get_the_ID,'');
            $query->the_post();
            setlocale(LC_TIME, 'ita', 'it_IT');
            if(get_post_meta(get_the_ID(), 'ovaem_date_start_time')[0] < date("Y-m-d")) {
                $ret['schedule'][] = [
                    'date' => date('Y-m-d',get_post_meta(get_the_ID(), 'ovaem_date_start_time')[0]),
                    'groups' => [[
                        'time' => utf8_encode( strftime("%d %b %Y", get_post_meta(get_the_ID(), 'ovaem_date_start_time')[0]) ),
                        'sessions' => [[
                            'name' => html_entity_decode(the_title('','',false), ENT_QUOTES),
                            'location' => get_the_terms( get_the_ID(), 'location')[0]->name,
                            'description' => get_the_content(),
                            'timeStart' => date('d M Y h:i',get_post_meta(get_the_ID(), 'ovaem_date_start_time')[0]),
                            'timeEnd' => date('d M Y h:i',get_post_meta(get_the_ID(), 'ovaem_date_end_time')[0]),
                            'tracks' => [ get_the_terms( get_the_ID(), 'categorie' )[0]->name ],
                            'id' => get_the_ID(),
                            'pic' => get_the_post_thumbnail_url(),
                        ]]
                    ]]
                ];
            }

        }
    }
}

数据正确返回但格式不正确,希望有人能提供帮助

标签: phpjsonwordpress

解决方案


推荐阅读