2017-04-10 11 views
0

私はAjaxコールを練習しており、返されたJSONデータにアクセスする際に問題が発生しています。jqueryでjsonデータを操作する

私はちょうど出力に私のJSONオブジェクトのcontentlinkプロパティをしようとしている

[ 
{ 
"ID": 1127, 
"title": "Paul Rand", 
"content": "<p>Good ideas rarely come in bunches. The designer who voluntarily presents his client with a batch of layouts does so not out prolificacy, but out of uncertainty or fear. </p>\n", 
"link": "https://quotesondesign.com/paul-rand-7/" 
} 
] 

を出力

$('button').on('click', function() {  
    $.ajax({ 
    url: 'http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1', 
    success: function(data) { 
     console.log(data); 
    }, 
    error: function() { 
     console.log('error occured'); 
    }, 
    cache: false 
    }); 
}); 

の下に次のコードを持っています。私は、次のことを試してみた:

[{"ID":2294,"title":"Josh Collinsworth","content":" 
You do not need to have a great idea before you can begin working; you need to begin working before you can have a great idea. 

\n","link":"https://quotesondesign.com/josh-collinsworth-3/"}] 

を出力

$('.message').html(JSON.stringify(data)); 

私はJSONデータを操作するための標準的な方法は、すべての助けに感謝を探すためにしようとしています!

答えて

3

名前が示すように、stringifyはJSONを文字列に変換します。 JSONは、ネイティブのJavaScriptで、あなたのサンプルデータに基づいて:

[ 
    { 
     "ID": 1127, 
     "title": "Paul Rand", 
     "content": "<p>Good ideas rarely come in bunches. The designer who voluntarily presents his client with a batch of layouts does so not out prolificacy, but out of uncertainty or fear. </p>\n", 
     "link": "https://quotesondesign.com/paul-rand-7/" 
    } 
] 

あなたは(中括弧で。)オブジェクトの(角括弧内)の配列を取り戻すこの場合、それは、配列に一つだけのオブジェクトだ、あなたので、 data[0]で配列の最初のオブジェクトにアクセスして、そのcontentプロパティを取得:

$('.message').html(data[0].content); 
+0

uはインデックス0を明確にすることができますか? –

+0

はい、質問 – miken32

+0

ああを編集します。私は今すべてをはっきりと見ることができます。助けてくれてありがとう!!! APIを使って作業することは既に素晴らしいです!!!! –