1

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.

karthick
  • 5,998
  • 12
  • 52
  • 90

2 Answers2

2

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, ','));
Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data while second time parsing – karthick Jun 07 '13 at 08:05
  • @Swift you're wrong - I did realise it, but the OP wanted the objects from the two arrays to be merged into a _single_ array. – Alnitak Jun 07 '13 at 08:23
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.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • that first `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. – Alnitak Jun 07 '13 at 08:12
  • @karthick.k I don't see how that can work when the `var res` you've supplied isn't even legal JS. – Alnitak Jun 07 '13 at 08:14
  • @Alnitak - good catch, thanks. I'd changed the outermost `"` to `'` in my test case but forgot to copy it over to the answer that way. – Michael Geary Jun 07 '13 at 08:14
  • @MichaelGeary it's unclear whether the outer double quotes the OP put in the question ever even existed. – Alnitak Jun 07 '13 at 08:15
  • @Alnitak - True, but the code I posted would work the same with or without them. – Michael Geary Jun 07 '13 at 08:16