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.

8
  • can you just make the file node.json and just store the json, and use ajax to call it??? Commented Jun 7, 2013 at 8:00
  • @JeffLee I am working only on retrieval part, No write permission Commented Jun 7, 2013 at 8:01
  • @karthick.k, show ajax call Commented Jun 7, 2013 at 8:02
  • Possible duplicate: stackoverflow.com/questions/5801699/… Commented Jun 7, 2013 at 8:02
  • btw var res is not a valid javascript statement???? Commented Jun 7, 2013 at 8:04

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@karthick.k I don't see how that can work when the var res you've supplied isn't even legal JS.
@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.
@MichaelGeary it's unclear whether the outer double quotes the OP put in the question ever even existed.
@Alnitak - True, but the code I posted would work the same with or without them.
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, ','));

2 Comments

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data while second time parsing
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.