【发布时间】:2016-04-07 08:16:08
【问题描述】:
我正在使用 wp-rest api,我有这个 json 结构可以使用:
[
{
"id": 11,
"title": {
"rendered": "Test-Font"
},
"acf": {
"schrift": [
{
"zeichen": "A",
"anzahl": "23"
},
{
"zeichen": "B",
"anzahl": "46"
},
{
"zeichen": "C",
"anzahl": "42"
},
{
"zeichen": "D",
"anzahl": "49"
},
{
"zeichen": "E",
"anzahl": "31"
},
…
{
"id": 12,
"title": {
"rendered": "Test-Font2"
},
"acf": {
"schrift": [
{
"zeichen": "A",
"anzahl": "20"
},
{
"zeichen": "B",
"anzahl": "12"
},
…
我需要加载此结构的不同帖子,并为每个帖子创建一个对象,其中包含有关 id 和字体详细信息(通过 acf 存储的数据)的信息
目前我有这个脚本:
var schrift = {}
jQuery(function($){
$.ajax({
url: 'http://localhost/wordpress-dev/mi/wp-json/wp/v2/schriften/',
type: "GET",
dataType: "json",
success: function (data) {
$.each(data, function() {
schrift.id = this.id;
$.each(this.acf.schrift, function(key, value) {
schrift.value = value;
});
console.log (schrift);
});
}
})
})
但是在新对象中只存储了一个信息,我如何遍历整个 json 结构并创建/存储它以使用它。
我需要的是:对于每个加载的帖子:关于 id 的信息和使用 acf 存储的详细信息。
非常感谢
编辑:
是否可以在一个js对象中重构json对象,其功能类似于关联数组:
这样
11 //ID
A // zeichen
23 // anzahl
B
46
C
42
这样就可以得到这样的值:
object['11'].A // should get 23
【问题讨论】:
-
为什么不将每个
schrift推入一个schriften数组中? -
为什么要复制到新对象?只需使用
data对象,它拥有您需要的一切。
标签: jquery json ajax wordpress rest