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条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器