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=
Solution 1:
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.
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 inputvar input = $('#key').val();
// Retrieve the property using the user input stringvar output = data[input];
// Placeholder objectvar 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.
Solution 2:
Please find the answer $(data.results).each(function() {
var output = "<p>" + this["link_1/_text"] + "</p>";
$('#placeholder').append(output);
});
Post a Comment for "Javascript Json Output With Slash "/""