duanlan7903 2013-03-04 17:55
浏览 127
已采纳

如何将数值转换为字符串?

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 [ ]

  • 写回答

2条回答 默认 最新

  • dongyi2159 2013-03-04 17:59
    关注

    Convert numbers to strings:

    var json = [[text_var,number_var.toString(),number_var.toString()]];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?