我正在尝试根据通过Wordpress返回的JSON数据创建一个类别数组。目前的输出是:
"property_categories_0_property_category":[
"All"
],
"property_categories_1_property_category":[
"Hospitality"
],
"property_categories_2_property_category":[
"Retail"
],
"property_categories_3_property_category":[
"Outdoor Advertising"
],
"property_categories":[
"4"
]
我的JSON电话是:
$.ajax({
url: '?json=1',
type: "POST",
success: function(data){
var fields = data.page.custom_fields;
for (var i = 0; i < fields.property_categories[0]; i++) {
categories = fields.property_categories_ + i + _property_category[0];
}
}
});
由于尝试连接for循环中的键值,我收到错误。
有没有办法实现这个目标?
完整的JSON输出:
{
status: "ok",
page: {
id: 90,
type: "page",
slug: "properties",
url: "http://delcodev:8888/properties/",
status: "publish",
title: "Properties",
title_plain: "Properties",
content: "<div class="map"><div class="marker" data-property-selector="hospitality" data-lat="39.9312311" data-lng="-74.95873819999997"><a style="display:block;" href="http://delcodev:8888/properties/aloft-mount-laurel/">aloft Mount Laurel</a><p>Morbi risus augue, maximus eget pulvinar sed, laoreet ac nulla. Donec et nunc ac ex rhoncus suscipit vel eu orci. Proin nunc nulla, iaculis eget condimentum quis, tincidunt et nisi. In ultrices, massa nec mattis semper, massa odio vulputate ipsum, vitae sollicitudin urna nisl a ante. In hac habitasse platea dictumst. Cras finibus volutpat tortor, … <a href="http://delcodev:8888/properties/aloft-mount-laurel/" class="more-link">Continue reading <span class="screen-reader-text">aloft Mount Laurel</span></a></p></div><div class="marker" data-property-selector="outdoor advertising" data-lat="39.9195342" data-lng="-75.14346219999999"><a style="display:block;" href="http://delcodev:8888/properties/lowes/">Lowes South Philadelphia</a><p>Donec bibendum lacus urna, sit amet vehicula nibh imperdiet non. Sed consequat diam et odio molestie, sed faucibus orci fringilla. Proin suscipit sit amet felis mattis venenatis. Pellentesque sit amet feugiat metus. Duis id sapien egestas, eleifend est sagittis, egestas tortor. Pellentesque quis mi vitae augue sodales commodo. In nec efficitur ante. Pellentesque et pharetra … <a href="http://delcodev:8888/properties/lowes/" class="more-link">Continue reading <span class="screen-reader-text">Lowes South Philadelphia</span></a></p></div><div class="marker" data-property-selector="retail" data-lat="39.9699176" data-lng="-74.91403300000002"><a style="display:block;" href="http://delcodev:8888/properties/starbucks-mount-laurel/">Starbucks Mount Laurel</a><p>Sed dignissim justo vitae sem gravida, ac sodales justo suscipit. Sed pulvinar iaculis magna, eu viverra nunc eleifend in. Phasellus bibendum dui at metus rhoncus, ac molestie enim tincidunt. Vivamus placerat ut sem non dignissim. Proin molestie id magna at interdum. Donec venenatis metus sit amet hendrerit consectetur. Maecenas commodo urna venenatis varius aliquet. Nunc … <a href="http://delcodev:8888/properties/starbucks-mount-laurel/" class="more-link">Continue reading <span class="screen-reader-text">Starbucks Mount Laurel</span></a></p></div><div class="marker" data-property-selector="retail" data-lat="39.948684" data-lng="-75.160755"><a style="display:block;" href="http://delcodev:8888/properties/starbucks-center-city/">Starbucks Center City</a><p>Integer sed nibh sit amet urna tristique pretium eget at arcu. Nulla nec enim interdum, suscipit ipsum at, rutrum orci. Phasellus est nibh, euismod nec ex a, malesuada semper metus. Duis ut blandit lectus, ac ullamcorper tortor. Phasellus vel porta augue, id maximus est. Proin aliquet euismod lacus, ut vestibulum leo dictum nec. Cum sociis … <a href="http://delcodev:8888/properties/starbucks-center-city/" class="more-link">Continue reading <span class="screen-reader-text">Starbucks Center City</span></a></p></div></div> ",
excerpt: "",
date: "2015-11-19 18:57:45",
modified: "2015-11-24 21:44:48",
categories: [ ],
tags: [ ],
author: {
id: 1,
slug: "delcodev",
name: "delcodev",
first_name: "",
last_name: "",
nickname: "delcodev",
url: "",
description: ""
},
comments: [ ],
attachments: [ ],
comment_count: 0,
comment_status: "closed",
custom_fields: {
client_logo_grid_0_client_logo: [
"39"
],
client_logo_grid_1_client_logo: [
"38"
],
client_logo_grid_2_client_logo: [
"35"
],
client_logo_grid_3_client_logo: [
"39"
],
client_logo_grid_4_client_logo: [
"36"
],
client_logo_grid_5_client_logo: [
"34"
],
client_logo_grid: [
"6"
],
mp4_video: [
"14"
],
webm_video: [
"15"
],
video_poster: [
"16"
],
property_categories_0_property_category: [
"All"
],
property_categories_1_property_category: [
"Hospitality"
],
property_categories_2_property_category: [
"Retail"
],
property_categories_3_property_category: [
"Outdoor Advertising"
],
property_categories: [
"4"
]
}
}
}
答案 0 :(得分:1)
你没有连接任何键,你在这里做的是一个总和或连接字符串,其中fields.property_categories_
是一个属性而i
是另一个,_property_category[0]
是另一个,你必须做什么使用 查找模板 / 括号表示法 ,如下所示:
var fields = data.page.custom_fields;
for (var i = 0; i < fields.property_categories[0]; i++) {
categories = fields['property_categories_' + i + '_property_category'][0];
}
例如:
obj[property_name]