具有高级自定义字段的Wordpress Rest API Google Map字段返回奇怪的字符串 [英] Wordpress Rest API with advanced custom fields Google map field returns weird string
问题描述
我想使用具有高级自定义字段的WordPress后端和JSON Wordpress API将数据提供给移动应用程序.
I want to use a Wordpress backend with advanced custom fields and the json Wordpress API to serve data to a mobile app.
对于此项目,客户需要在地图自定义字段中选择一个位置.该API应该返回一个json数组,其中包含一些其他数据以及Google Maps字段中的数据.
For this project the client needs to select a location in the maps custom field. The API should return a json array with some other data and the data from the Google maps field.
API返回的对象的格式如下:
The object the API returns is formatted as follows:
{
"0": [
{
"title": "Test 1",
"evenement_afbeelding": "38",
"kunstenaars": "Name One",
"openingstijden": "9:00 - 18:30",
"evenementbeschrijving": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ac pharetra tortor.",
"evenement_lokatie": "a:3:{s:7:\"address\";s:40:\"Jansweg 50, 2011 KN Haarlem, Netherlands\";s:3:\"lat\";s:17:\"52.38513460768028\";s:3:\"lng\";s:17:\"4.638633728027344\";}",
"biografie": "<strong>Lorem ipsum dolor sit amet</strong>\r\n\r\nConsectetur adipiscing elit. Nullam ac pharetra tortor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos."
}
],
"status": "ok"
}
奇怪的是,键"evenement_lokatie"(Google Maps字段)上的值返回了我无法转换为数组的字符串. "evenement_lokatie"键上的值应包含另一个数组,如下所示:
The weird thing is that the value on key "evenement_lokatie" (the Google maps field) returns a string that I cannot convert to an array. The value on the "evenement_lokatie" key should contain another array like this:
{
"0": [
{
"title": "Test 1",
"evenement_afbeelding": "38",
"kunstenaars": "Name One",
"openingstijden": "9:00 - 18:30",
"evenementbeschrijving": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ac pharetra tortor.",
"evenement_lokatie": [
{
"Stad": "Haarlem",
"lang": "32143241",
"lat": "721321"
}
],
"biografie": "<strong>Lorem ipsum dolor sit amet</strong>\r\n\r\nConsectetur adipiscing elit. Nullam ac pharetra tortor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos."
}
],
"status": "ok"
}
这是应该在wordpress中格式化数据的PHP代码:
This is the PHP code that should format the data in wordpress:
<?php
class json_api_events_controller{
public function hello_world(){
return array('message'=>'Hello World!');
}
public function get_events(){
$array = array();
$args = array('post_type'=>'evenementen', 'posts_per_page'=>-1, 'order_by'=>'title', 'order'=>'ASC');
$loop = new WP_Query($args);
$counter = 0;
while ( $loop->have_posts() ) : $loop->the_post();
$id = get_the_id();
$custom = get_post_custom($id);
// add the title
$array[$id][$counter]['title'] = get_the_title();
// add all the custom fields
foreach($custom as $k => $v){
// if key starts with '_' symbol, don't add to the array
if(strpos($k, '_') !== 0) {
$array[$id][$counter][$k] = array_shift($v);
}
}
$counter++;
endwhile;
return $array;
}
}
字符串中也有奇怪的字符,例如; s:17:"和; s:40:". 谁能告诉我如何将这样的字符串转换为我所描述的数组?
Also there are weird chracters in the string, like ";s:17:" and ";s:40:". Can anyone show me how I can convert such a string to an array like I described?
提前谢谢!
推荐答案
怪异"值实际上是PHP的序列化-您可以使用之前,先执行function.unserialize.php"rel =" nofollow>反序列化 json_encode ,就可以了.
The "weird" value is actually serialized PHP - you can deserialize it serverside using unserialize before you json_encode, and you should be fine.
Wordpress以这种方式序列化元数据,以便可以将任何对象另存为字符串.诀窍是在创建有效的JSON对象之前,使用PHP反序列化获取有效的PHP对象.
Wordpress serializes metadata in this way, so that any object can be saved as strings. The trick is to use PHP deserialization to get a valid PHP object before creating the valid JSON object.
希望这会有所帮助.
这篇关于具有高级自定义字段的Wordpress Rest API Google Map字段返回奇怪的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!