converts string into JSON format
str | string | string to convert |
string | string in JSON format |
var t = window.dhx.s2j('{my_key: "my_value"}');
console.log(t.my_key); // my_value
It can be used for parsing server responses:
<?php
// server side
if (isset($_REQUEST["action"]) && $_REQUEST["action"] == "aboutMe") {
print_r('{name: "James", age: 26, role: "Site Admin"}');
}
?>
// client side
window.dhx.ajax.get("server.php?action=aboutMe", function(r){
var t = window.dhx.s2j(r.xmlDoc.responseText);
if (t != null) {
for (var a in t) console.log(a+": "+t[a]);
} else {
dhtmlx.message({type: "error", text: "...."});
}
});
the result will be:
name: James
age: 26
role: Site Admin