Reputation: 542
I have followig html:
<span>
Array
(
[namePerson/first] => firstnamedata
[namePerson/last] => lastnamedata
[namePerson] => firstnamedata lastnamedata
[person/gender] => genderdata
)
</span>
Can I somehow achieve something like this?
var myArray = $("span").text();
alert(myArray["namePerson/first"]);
I mean to litterally interpret the string as an array and access it's keys/values.
Upvotes: 0
Views: 87
Reputation: 5930
You could write a basic parser.
function nextC(i, text) {
if (i < text.length)
return text.charAt(i);
return "";
}
function skipSpace(i, text) {
while (c == " ")
c = nextC(++i, text);
return i;
}
function parseObj(text) {
var lablel = "";
var value = "";
var obj = {};
for (var i = 0; i < text.length; i++) {
var c = text.charAt(i);
if (c == "[") {
while (c != "]") {
c = nextC(++i, text);
label += c;
}
} else if (c == " ") {
i = skipSpace(i, text);
c = nextC(i, text);
if (c == "=") {
c = nextC(++i, text);
if (c == ">" {
i = skipSpace(i, text);
c = nextC(i, text);
while (c != "\n") {
c = nextC(++i, text);
value += c;
}
obj[label] = value;
}
}
}
}
return obj;
}
var text = $("span").text();
var obj = parseObj(text);
alert(obj["namePerson/first"]);
Note This may be prone to bugs. If you are trying to transfer data from server to client, try using JSON or XML.
Upvotes: 1