2016-07-04 9 views
1

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.

+0

Ihr JSON wird nur eine Zeichenfolge sein, bis Sie es analysieren, richtig? http://api.jquery.com/jquery.parsejson/ – Nick

Antwort

6

Sie müssen JSON.parse die Daten. In Ihrem Fall wird es nicht als application/json gesendet, es wird als text/html gesendet.

Ihr Erfolg Funktion so etwas tun sollte:

success: function(data){ 
    data = JSON.parse(data); 
    console.log(Object.keys(data).length); 
} 

Als Alternative Sie dies obwohl versuchen können, wenn Sie die Daten JSON.parse wollen nicht und haben gerade jQuery es verwalten . Sie können die dataType Sie erwarten vom Server in der Config-Objekt übergeben:

$.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); 
    }, 
    dataType: 'json' // <- Add this 
}); 

Hier die documentation for $.ajax ist, die für diesen Schlüssel zu den Optionen spricht.


Und als letzte Option kann Sie $.getJson() stattdessen verwenden, die eine Kurzversion des oben ist.

+1

Wie dieser Typ sagt ... der einfachste Weg, um es zu analysieren ist es, nur 'dataType: 'Json'' zu der Ajax Anfrage hinzuzufügen. –

+0

@KevBot Vielen Dank in dem Moment, als ich meine Android App öffnete, erinnerte ich mich daran, dass ich das Parsing machen musste. Danke für Ihre Lösungen und Antworten. –

Verwandte Themen