0

I have a JS function which I'm passing some JSON. I produce this JSON with PHP:

<?php

$array[] = array('hello','123','456');

echo json_encode($array);

?>

This gives me something neat for JS like:

[["hello","123","456"]]

My JS function seems to like this format.

The problem I have however is when I produce a similar array directly in JS like so:

var json = [[text_var,number_var,number_var]]; // these vars established earlier in the code

var json_stringify = JSON.stringify(json);

I wind up with something that looks like this:

[["hello",123,456]]

So basically, it isn't encapsulating my numbers with quotes. I wouldn't think it would matter, but without them I get a Uncaught RangeError: Maximum call stack size exceeded

I tried doing something like preparing the JSON myself with:

var json = '[["'+place_name+'","'+longitude+'","'+latitude+'"]]';

But passing that it doesn't like either - maybe it isn't feeling it as a JSON var, and I've tried parsing that through JSON.stringify but I end up with backslashes before every quote and it doesn't like that either.

What is my feeble mind not seeing here?

EDIT: My PHP array is in another array hence the result of [["hello',"123","456"]] and not single brackets [ ]

Aaron Kurtzhals
  • 2,036
  • 3
  • 17
  • 21
willdanceforfun
  • 11,044
  • 31
  • 82
  • 122
  • 3
    Convert your numbers to strings. – datasage Mar 04 '13 at 17:56
  • 1
    The numbers in your PHP code are strings because they are wrapped in the single quotes: `123`, `456`. The numbers in your JavaScript code are simply numbers. If you want them to be strings the you should use the JavaScript `.toString()` method to convert them to strings (i.e. `number_var.toString()`). – War10ck Mar 04 '13 at 18:00
  • 1
    Are you sure it's [["hello","123","456"]], not ["hello","123","456"] ? The former is an array with a single element which is itself an array of strings. The latter is an array of strings, which matches your PHP. Besides that, you're getting perfectly valid JSON. The function you're sending it to is wrong (or possibly expecting the second format I mentioned). – Dave Mar 04 '13 at 18:00
  • 1
    actually, looking again, the PHP you quoted should produce: {'json':['hello','123','456']} which is rather different. – Dave Mar 04 '13 at 18:02
  • @Dave sorry, I misquoted myself!! Thanks for picking that up - I've edited the post. The outcome is definitely double brackets because the actual JSON has many arrays in the array. – willdanceforfun Mar 04 '13 at 18:57
  • Looks like this question is a duplicate of http://stackoverflow.com/questions/5765398/whats-the-best-way-to-convert-a-number-to-a-string – Aaron Kurtzhals Mar 04 '13 at 19:00

2 Answers2

1

JSON supports numbers (without quotes) and strings (with quotes). To force it to use strings, try:

var json = [[text_var,number_var + "",number_var + ""]]; 
SteveP
  • 18,840
  • 9
  • 47
  • 60
1

Convert numbers to strings:

var json = [[text_var,number_var.toString(),number_var.toString()]];
Pitchinnate
  • 7,517
  • 1
  • 20
  • 37