简体   繁体   中英

Javascript Json output with slash “/”

i have this function using json and 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);
});

i just wanna print one data that from this "link_1/_text":"kebahagiaan" that is kebahagiaan

can you explain to me how it should be?

Thankyou

Here is an updated fiddle

You can use the indexer based syntax to get the result you want like this:

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

In JavaScript, objects act as a kind of key/value store so you can access the property name directly through a string. While you can access them with the dot . notation, it does not allow you to do so with characters that are invalid in a variable name.

A great use of this which makes code reflection easier is if you have a string containing the name of the property you want to retrieve.

Here's a little example

HTML

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

Script

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

This basically takes the user input and outputs the property matching that string.

This example is not very valuable, but the technique itself is quite useful indeed.

Please find the answer $(data.results).each(function() {

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM