Creating JSON without the use of any external library? [duplicate] - javascript

This question already has answers here:
Encoding Javascript Object to Json string
(2 answers)
Closed 6 years ago.
SORRY! EDIT ON OLD BROWSERS prior to ie8!
In order to create XML on the fly in JS, one can do the below,
Is there any way I can achieve the same - creating JSON on the fly in JS without the use of any external library?
var parent = document.createElement("parent");
var children = document.createElement('children');
var child1 = document.createElement('child1');
var child2 = document.createElement('child2');
var textNode1 = document.createTextNode("some text1");
var textNode2 = document.createTextNode("some text2");
child1.appendChild(textNode1 );
child2.appendChild(textNode2 );
children.appendChild(child1);
children.appendChild(child2);
parent.appendChild(children);
alert(parent.outerHTML);

Just use JSON.stringify()
i.e.
var o={a:12};
JSON.stringify(o);
results in
"{"a":12}"

Related

How to retrieve setAttribute from servlet into JavaScript [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
I'm trying to retrieve the value of my setAttribute into my javascript code but I get a syntax error when I run the page.
Here is the code of my setAttribute in my servlet:
request.setAttribute("codeIGA", codeIGA); //a string is being passed here
Here is the code of my JS:
$(document).ready(
function () {
var count = 0;
var lastCodeIGA = ${sessionScope.codeIGA}; // syntax error here
console.log(lastCodeIGA);
.
.
.
etc
How can I retrieve the value from my servlet and input it into my JS?
You can use following to retrieve the required value.
var lastCodeIGA = '${codeIGA}';
Try with scriptlet.
var test = <%=request.getAttribute("codeIGA");
Or
var lastCodeIGA = '${codeIGA}';

Parse JSON in Ext JS 4 or JavaScript [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 10 years ago.
I have this type of JSON:
{"value":[{"idProductCategoryAttributeValue":43,"value":"7","sortOrder":0}]}
I want the individual parameters values like. How do I parse this JSON string in Ext JS 4 or in simple JavaScript?
Have a look at http://docs.sencha.com/ext-js/4-1/#!/api/Ext.JSON. There you find how you can parse JSON with Ext JS 4.
var strJson = '{"value": [{"idProductCategoryAttributeValue":43,"value":"7","sortOrder":0}]}';
var obj = Ext.JSON.decode(strJson);
var obj = Ext.decode(jsonstr);

empty jquery element [duplicate]

This question already has answers here:
Getting an empty JQuery object
(4 answers)
Closed 8 years ago.
Is there an elegant way to create an empty jquery element (versus null) ?
$myEmptyElement = $("#ThisIdDoesNotExist234343");
The rational is not checking later on for null.
Later we do :
$myEmptyElement.destroy();
Sure:
var $emptyElement = $();
Why would you want one, though?
A simple example would be:
var $emptyElement = $();
One way:
var $emptyElement = new $;

How to pass global JS var within regexp [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you pass a variable to a Regular Expression JavaScript?
This is my regexp
regexp:{
spamcheck : /^[15]+$/
}
I need to do this
var spamcheck_sum = 15;
regexp:{
spamcheck : /^[spamcheck_sum]+$/
}
and
Side note: I cant use my spamcheck_sum var outside the js reason is my js file loads first and my var for sum loads after , any way to globalize that var to be acceptable no matter if js file loads first?
working with cms here and js file placement is automatic if I use the cms js placements calls . Joomla
var spamcheck_sum = 15;
regexp:{
spamcheck : new RegExp("^["+spamcheck_sum+"]+$")
}

Convert JSON string to Javascript array [duplicate]

This question already has answers here:
Convert a multidimensional javascript array to JSON?
(9 answers)
Closed 2 years ago.
I have an array of arrays which I have planted in the DOM, so that I can reuse it at a later stage to transfer to the server:
[["1","aaaaaa","1"],["2","bbbbbbb","2"],["3","ccccccc","3"]]
If I would like to convert it back into a Javascript array, how would I go about doing this?
var obj = $.parseJSON('[["1","aaaaaa","1"],["2","bbbbbbb","2"],["3","ccccccc","3"]]')
Assuming jquery is ok to use because of tag.
If the browzer has the JSON object then
JSON.parse(string);
or if you have jQuery
$.parseJSON(string);
var array = JSON.parse(my_JSON)
jQuery.parseJSON()
var myArr = $.parseJSON(myJsonString);

Categories

Resources