I am reading the plain text file in node.js using ajax call from client side.
Result :
success gives the result as below.
""[{\"x\":233,\"y\":279,\"count\":1},{\"x\":256,\"y\":243,\"count\":6}]\n[{\"x\":233,\"y\":279,\"count\":1},{\"x\":256,\"y\":243,\"count\":6}]\n""
After parsing the above result :
JSON.parse(result);
"[{"x":233,"y":279,"count":1},{"x":256,"y":243,"count":6}] [{"x":233,"y":279,"count":1},{"x":256,"y":243,"count":6}] "
I want to change this string to array of objects,
Expected Result is array of objects:
[{"x":233,"y":279,"count":1},{"x":256,"y":243,"count":6},
{"x":233,"y":279,"count":1},{"x":256,"y":243,"count":6}
]
Ajax call Code
$.ajax({
url: document.URL + "getData",
method : "GET",
success: function (result) {
var info = JSON.parse(result);
var object = JSON.parse(info);
console.log(object);
}
});
Any idea will be helpful.
-
can you just make the file node.json and just store the json, and use ajax to call it???Jeff Lee– Jeff Lee2013年06月07日 08:00:04 +00:00Commented Jun 7, 2013 at 8:00
-
@JeffLee I am working only on retrieval part, No write permissionkarthick– karthick2013年06月07日 08:01:46 +00:00Commented Jun 7, 2013 at 8:01
-
@karthick.k, show ajax callmaximkou– maximkou2013年06月07日 08:02:00 +00:00Commented Jun 7, 2013 at 8:02
-
Possible duplicate: stackoverflow.com/questions/5801699/…David Jashi– David Jashi2013年06月07日 08:02:11 +00:00Commented Jun 7, 2013 at 8:02
-
btw var res is not a valid javascript statement????Jeff Lee– Jeff Lee2013年06月07日 08:04:59 +00:00Commented Jun 7, 2013 at 8:04
2 Answers 2
That is some seriously messed-up JSON. There's an extra quote mark at each end, and ]\n[ in the middle where there should be a ,.
You really should fix your server to generate valid JSON, but if you can't, you could tweak it like this:
var res = '"[{\"x\":233,\"y\":279,\"count\":1},{\"x\":256,\"y\":243,\"count\":6}]\n[{\"x\":233,\"y\":279,\"count\":1},{\"x\":256,\"y\":243,\"count\":6}]\n"';
var resfix = res.replace( /^"|"$/g, '' ).replace( ']\n[', ',' );
JSON.parse( resfix );
I changed the extra set of quotes at the very outside of your var res = string to make it a valid JavaScript string for testing.
5 Comments
var res line copied from the OP isn't legal. If the enclosing double quotes aren't really there then the first .replace() is unnecessary.var res you've supplied isn't even legal JS." to ' in my test case but forgot to copy it over to the answer that way.It looks to me like you've simply got double-encoded JSON. Just run it through JSON.parse() a second time.
EDIT actually, that's not quite right - the output contains two JS arrays, with a \n separator and no enclosing array. You will have to manipulate the data a bit (looking at that now) to make it parseable a second time.
EDIT2 This appears to work, albeit that your current var res line includes an extra pair of surrounding quotes that aren't legal so in this test I've removed them:
var res = "[{\"x\":233,\"y\":279,\"count\":1},{\"x\":256,\"y\":243,\"count\":6}]\n[{\"x\":233,\"y\":279,\"count\":1},{\"x\":256,\"y\":243,\"count\":6}]\n"
var out = JSON.parse(res.replace(/]\s*\[/g, ','));