带有斜杠"/"的Javascript Json输出; [英] Javascript Json output with slash "/"

查看:178
本文介绍了带有斜杠"/"的Javascript Json输出;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有使用json和jquery的功能

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

我只想从"link_1/_text"中打印一个数据:"kebahagiaan" 那是 kebahagiaan

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?

谢谢

推荐答案

此处是更新的小提琴

您可以使用基于索引器的语法来获取所需的结果,如下所示:

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

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

在JavaScript中,对象充当一种键/值存储,因此您可以直接通过字符串访问属性名称.虽然您可以使用点.表示法来访问它们,但不允许使用变量名中无效的字符来访问它们.

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.

这是一个小例子

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

这基本上需要用户输入并输出与该字符串匹配的属性.

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.

这篇关于带有斜杠"/"的Javascript Json输出;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆