2016-11-23 4 views
-1

私はこのツールを使っていますhttp://apps.evemilano.com/entities/ APIに基づいています。 これは私が取得するJSONファイルです。すべての "itemListElement"が同じレベルの詳細を持っているわけではありません。私はすべてのItemListを "記述"で数えたいと思う。JSON内の既存の要素のみを数える方法

{ 
    "@context": { 
    "@vocab": "http://schema.org/", 
    "goog": "http://schema.googleapis.com/", 
    "EntitySearchResult": "goog:EntitySearchResult", 
    "detailedDescription": "goog:detailedDescription", 
    "resultScore": "goog:resultScore", 
    "kg": "http://g.co/kg" 
    }, 
    "@type": "ItemList", 
    "itemListElement": [ 
    { 
     "@type": "EntitySearchResult", 
     "result": { 
     "@id": "kg:/g/121gpq6x", 
     "name": "Calogero Angelo Sacheli", 
     "@type": [ 
      "Person", 
      "Thing" 
     ], 
     "description": "Author" 
     }, 
     "resultScore": 7.989742 
    }, 
    { 
     "@type": "EntitySearchResult", 
     "result": { 
     "@id": "kg:/m/0z3rcdq", 
     "name": "Giovanni Sacheli", 
     "@type": [ 
      "Thing", 
      "Person" 
     ], 
     "url": "http://www.evemilano.com/" 
     }, 
     "resultScore": 6.925036 
    }, 
    { 
     "@type": "EntitySearchResult", 
     "result": { 
     "@id": "kg:/m/07zy0l", 
     "name": "Quarterback Princess", 
     "@type": [ 
      "Movie", 
      "Thing" 
     ], 
     "description": "1983 film", 
     "detailedDescription": { 
      "articleBody": "Quarterback Princess is a 1983 American made-for-television fact-based sports drama film by 20th Century Fox that chronicles the courage and determination of a teenage girl who struggles against sexism and fights to play on her high school football team. ", 
      "url": "https://en.wikipedia.org/wiki/Quarterback_Princess", 
      "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License" 
     } 
     }, 
     "resultScore": 0.945328 
    } 
    ] 
} 

すべてのエンティティに1つの説明がありますが、3つのエンティティと2つの「説明」しかありません。どのように私はPHPを使用して既存の "記述"だけをCOUNTできますか?私は "2"を印刷する必要があります:)

ありがとう。

+0

はStackOverflowのへようこそ。あなたの質問に関連性の高いタグを追加してください。おそらく、特定のプログラミング言語を使用したソリューションが必要になります。これをタグとして追加します。 – Codo

+0

あなたはどの言語に興味がありますか?あなたはプログラミング言語やライブラリごとに異なる答えを得るかもしれません – Tudor

+0

申し訳ありませんが、PHP言語 –

答えて

0

あなたはjson_decodeであなたのJSONペイロードをデコードすることができます。

$data = json_decode($json, true); 

次にあなたが$data['itemListElement']を反復処理し、$item['result']['description']が定義されているかどうかを確認することができます

$count = array_reduce($data['itemListElement'], function($count, $item) { 
    return isset($item['result']['description']) ? ++$count : $count; 
}, 0); 

echo $count; // 2 
+0

スーパー、たくさんありがとう! –

関連する問題