I have the following JSON file:
({
"text1": "item_1",
"text2": "item_2",
"text3": "item_3",
"text4": "item_4"
})
How can I print the value of text2
?
Do jQuery will be fastest than pure JS ?
Thanks a lot.
I have the following JSON file:
({
"text1": "item_1",
"text2": "item_2",
"text3": "item_3",
"text4": "item_4"
})
How can I print the value of text2
?
Do jQuery will be fastest than pure JS ?
Thanks a lot.
Once that object is assigned to a variable (say foo
) you'd be able to print text2
value by doing:
var foo = {
"text1": "item_1",
"text2": "item_2",
"text3": "item_3",
"text4": "item_4"
};
console.log(foo.text2);
Regarding how faster is JS over jQuery, this is a very simple instruction and there won't be a significant difference between one or the other.
var strJSON = '{ "text1": "item_1", "text2": "item_2", "text3": "item_3", "text4": "item_4" }';
var objJSON = eval("(function(){return " + strJSON + ";})()");
alert(objJSON.text2);
$.getJSON("test.json", function(json) {
console.log(json); // this will show the info it in console
console.log(json.text2); //should print item_2 in console
});
pure JS is faster if you know how to write it properly. Jquery add a lot of nice features for developers but it costs CPU and memory.
This example above uses jQuery (hence the $)