Ich versuche, einen benutzerdefinierten Controller für das WordPress-JSON-API-Plugin zu erstellen und bis jetzt funktioniert alles außer die JSON-Daten, die ich habe, ist nicht im richtigen Format.Wie formatiere ich die JSON-Ausgabe auf das gewünschte Format
Dies ist meine aktuelle JSON Ausgabe:
{
"status": "ok",
"all_tags": {
"tag-1": {
"term_name": "Tag 1",
"category_details": {
"0": {
"category_ID": 8,
"category_name": "category 1",
"category_count": 2
},
"2": {
"category_ID": 13,
"category_name": "category 2",
"category_count": 1
}
}
},
"tag-2": {
"term_name": "Tag 2",
"category_details": [
{
"category_ID": 8,
"category_name": "category 1",
"category_count": 2
}
]
}
}
}
Um jedoch die Daten zu analysieren ich muss die JSON-Daten in einem bestimmten Format haben. Das richtige Format sollte wie folgt sein:
{
"status": "ok",
"all_tags": [
{
"id": 1,
"term_name": "Tag 1",
"category_details": [
{
"id": 2,
"category_ID": 8,
"category_name": "category 1",
"category_count": 2
},
{
"id": 3,
"category_ID": 13,
"category_name": "category 2",
"category_count": 1
}
]
},
{
"id": 2,
"term_name": "Tag 2",
"category_details": [
{
"id": 2,
"category_ID": 8,
"category_name": "category 1",
"category_count": 2
}
]
}
]
}
Dies ist, wie ich das Array für die json erschaffe:
<?php
...
$cats_all = array(); // the array
if (!isset($cats_all[$custom_term->slug])) {
// create the array
$cats_all[$custom_term->slug] = array(
'term_name' => $custom_term->name,
'category_details' => array(
array(
'category_ID' => $categories[0]->term_id,
'category_name' => $categories[0]->name,
'category_count' => $mycats[0]->category_count
)
)
);
} else {
$cats_all[$custom_term->slug]['category_details'][] = array(
'category_ID' => $categories[0]->term_id,
'category_name' => $categories[0]->name,
'category_count' => $mycats[0]->category_count
);
}
...
// remove duplicates
$input = $this->super_unique($cats_all);
// return the array for json output by the plugin
return array(
'all_tags' => $input,
);
Jede Hilfe wird sehr geschätzt. Auch der gesamte Controller kann viewed here sein.
Wie bestimmen Sie 'count' und' id' Werte? Sie scheinen nichts in Ihrer aktuellen JSON-Ausgabe zu entsprechen. Es scheint auch seltsam, es sollte doppelte ID-Werte geben ... – trincot
@trincot Ich habe nur eine der Standard-Controller-Ausgabe des Plugins verwendet, um zu zeigen, wie die Struktur sein sollte und vergaß, sie zu entfernen. –
OK, Sie haben 'count' entfernt, was ist mit den' id' Werten? Werden sie benötigt? – trincot