Hallo Ich habe ein Problem und ich habe versucht, ähnliche Dinge und zu beantworten, aber weder Object.keys (Daten) .length hilft mir nicht. Hier ist meine JSONJSON Länge in Javascript gibt die Anzahl der Zeichen anstelle der Anzahl der Array-Elemente
{
"POSTS": [
[
{
"term_id": 1,
"name": "Uncategorized",
"slug": "uncategorized",
"term_group": 0,
"term_taxonomy_id": 1,
"taxonomy": "category",
"description": "",
"parent": 0,
"count": 1,
"filter": "raw",
"cat_ID": 1,
"category_count": 1,
"category_description": "",
"cat_name": "Uncategorized",
"category_nicename": "uncategorized",
"category_parent": 0
},
{
"term_id": 2,
"name": "Nova",
"slug": "nova",
"term_group": 0,
"term_taxonomy_id": 2,
"taxonomy": "category",
"description": "",
"parent": 0,
"count": 1,
"filter": "raw",
"cat_ID": 2,
"category_count": 1,
"category_description": "",
"cat_name": "Nova",
"category_nicename": "nova",
"category_parent": 0
},
{
"term_id": 3,
"name": "nova1",
"slug": "nova1",
"term_group": 0,
"term_taxonomy_id": 3,
"taxonomy": "category",
"description": "",
"parent": 0,
"count": 1,
"filter": "raw",
"cat_ID": 3,
"category_count": 1,
"category_description": "",
"cat_name": "nova1",
"category_nicename": "nova1",
"category_parent": 0
},
{
"term_id": 4,
"name": "nova2",
"slug": "nova2",
"term_group": 0,
"term_taxonomy_id": 4,
"taxonomy": "category",
"description": "",
"parent": 3,
"count": 1,
"filter": "raw",
"cat_ID": 4,
"category_count": 1,
"category_description": "",
"cat_name": "nova2",
"category_nicename": "nova2",
"category_parent": 3
},
{
"term_id": 5,
"name": "nova3",
"slug": "nova3",
"term_group": 0,
"term_taxonomy_id": 5,
"taxonomy": "category",
"description": "",
"parent": 3,
"count": 1,
"filter": "raw",
"cat_ID": 5,
"category_count": 1,
"category_description": "",
"cat_name": "nova3",
"category_nicename": "nova3",
"category_parent": 3
}
]
],
"success": 1
}
Und hier ist mein Javascript-Code:
<select id ="new" name="new"></select>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
var select = document.getElementById("new");
$.ajax({
type: "POST",
url: "wp-content/themes/twentyfourteen/ajax.php", // this script returns my JSON
data: ({canum: 0}),
success: function(data){
console.log(Object.keys(data).length);
}
});
});
</script>
Das gibt mir 1445 als Länge nicht mehr als 5, wie ich brauche, wie in JSON
Anzahl der Segmente zu bekommen, wie 5 zu bekommen, anstatt Anzahl der Zeichen?
Können Sie mir bitte helfen? Vielen Dank.
Ihr JSON wird nur eine Zeichenfolge sein, bis Sie es analysieren, richtig? http://api.jquery.com/jquery.parsejson/ – Nick