2015-02-12 58 views
0

我有使用JSON和jQuery這個功能用斜槓 「/」

http://jsfiddle.net/7z4w6jxt/

var data = {"offset":0,"results":[{"link_1/_text":"kebahagiaan","link_3":"http://pmj.astaga.com/article/?p=414","link_1":"http://pmj.astaga.com/article/?tag=kebahagiaan","link_2":"http://pmj.astaga.com/article/?tag=meditasi","link_3/_text":"Meditasi: Makna Rasa Sakit","title_text":"Meditasi: Makna Rasa Sakit","text_2":"Semua manusia yang hidup di dunia ini ingin merasakan kebahagiaan, dalam bentuk apapun.","link_2/_text":"meditasi"},{"link_1/_text":"memberi dan menerima","link_3":"http://pmj.astaga.com/article/?p=411","link_1":"http://pmj.astaga.com/article/?tag=memberi-dan-menerima","link_2":"http://pmj.astaga.com/article/?tag=men-2","link_3/_text":"Take and Give","title_text":"Take and Give","text_2":"Untuk beberapa alasan yang sulit dimengerti, alam telah membagi pria dan wanita dalam sebuah perbedaan sikap dalam memandang sebuah hal.","link_2/_text":"men"},{"link_1/_text":"10 saran jika ingin menyatakan cinta","link_3":"http://pmj.astaga.com/article/?p=404","link_1":"http://pmj.astaga.com/article/?tag=10-saran-jika-ingin-menyatakan-cinta","link_2":"http://pmj.astaga.com/article/?tag=menyatakan-cinta","link_3/_text":"10 Saran Bagi Wanita Untuk Menyatakan Cinta Lebih Dulu","title_text":"10 Saran Bagi Wanita Untuk Menyatakan Cinta Lebih Dulu","text_2":"Apakah anda pernah menyukai seorang pria, dan dilihat dari gelagatnya sepertinya dia juga menyukai anda?","link_2/_text":"menyatakan cinta"}],"cookies":[],"connectorVersionGuid":"ed0ce142-861e-4d2e-bacd-3dd1de491a69","connectorGuid":"d6d21746-2d8f-4980-b1ec-8e1a5d52b133","pageUrl":"http://pmj.astaga.com/article/?page_id=709"}; 


$(data.results).each(function() { 
    var output = "<p>" + this.link_1_text + "</p>"; 
    $('#placeholder').append(output); 
}); 

我只是想打印一個數據,從這個 「LINK_1/_text」 的Javascript JSON輸出那就是kebahagiaan

你能向我解釋它應該是怎樣的嗎?

三江源

+0

使用基於索引器的語法'this [「link_1/_test」]' – 2015-02-12 04:58:16

回答

0

Here is an updated fiddle

您可以使用索引基於語法來得到結果,你想是這樣的:

"<p>" + this["link_1/_text"] + "</p"> 

在JavaScript中,對象作爲一種關鍵的/值存儲因此您可以通過字符串直接訪問屬性名稱。雖然您可以使用點號.來訪問它們,但它不允許您在變量名稱中使用無效的字符。

如果你有一個字符串包含你想要檢索的屬性的名字,這使得代碼反射更容易。

Here's a little example

HTML

<div id="placeholder"> 
</div> 
<input type="text" id="key" /> 
<button type="button" id="clicker">Submit</button> 

腳本

var data = { 
    prop1: "The value of property 1!", 
    prop2: "The value of property 2!", 
    prop3: "The value of property 3!", 
} 

$('#clicker').on('click', function(){ 

    // Get the user input 
    var input = $('#key').val(); 

    // Retrieve the property using the user input string 
    var output = data[input]; 

    // Placeholder object 
    var ph = $('#placeholder'); 

    if(typeof output === 'undefined'){ 
     // There is no property that matches the user input string 
     ph.html('Oops that property doesnt exist!'); 
    } 
    else{ 
     // There is a property, so lets write the value of it. 
     ph.html(output); 
    } 
}); 

這基本上把用戶輸入,並輸出屬性匹配的字符串。

這個例子不是很有價值,但技術本身確實非常有用。

+0

Verry很好的答案, – 2015-02-12 05:12:48

0

請找到答案 $(data.results)。每個(函數(){

var output = "<p>" + this["link_1/_text"] + "</p>"; 
$('#placeholder').append(output); 

});