2016-07-04 3 views
1

こんにちは私は問題があり、同様のことを試みましたが、答えましたがObject.keys(data).lengthはどちらも役に立ちません。ここに私のJSONはJSONの長さが配列の要素の数ではなく文字の数を返します

{ 
    "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 
} 

されており、ここに私のjavascriptのコードは次のとおりです。

<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> 

私は必要なので、これは代わりに5を取得する方法をJSON内のセグメントの数を取得する方法を、長さがない5として私に1445を返します。文字数の?

お願いします。ありがとうございました。

+0

あなたのjsonは解析するまで文字列になるでしょうか? http://api.jquery.com/jquery.parsejson/ – Nick

答えて

6

あなたはJSON.parseのデータが必要です。あなたの場合、それはapplication/jsonとして送信されません、それはtext/htmlとして送信されています。あなたはJSON.parseデータにしたくない場合は、しかし、これを試すことができますし、ちょうどjQueryのは、それを管理している 代替として

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

、:あなたの成功の機能は、このような何かを行う必要があります

。ここで

$.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 
}); 

は、そのキーのオプションについて語っdocumentation for $.ajaxです:あなたはdataType、あなたのconfigオブジェクト内のサーバーから戻って期待しているを渡すことができます。


そして、もう一つ最後のオプションとして、あなたは、上記の短縮バージョンである代わりに$.getJson()を使用することができます。

+1

この男が言うように、それを解析する最も簡単な方法は、ajaxリクエストに 'dataType: 'json''を追加することです。 –

+0

@KevBot非常に私は私のアンドロイドアプリを開いた瞬間、私は解析する必要があることを思い出してくれてありがとう。あなたのソリューションと答えをありがとう。 –

関連する問題