将JSON中的大数字parsing为string

我试图解决这个问题与JSON.parse()四舍五入大数字。 我知道为什么会发生这种情况,但我正在寻找它。

我正在想一个正则expression式,它可以parsingJSON文本,并把所有的大整数转换成string。

我在Windows 8应用程序中使用JavaScript,它必须处理客户端。

让我难倒了迄今。

主要问题是我收到来自XMLHTTPRequest的响应后,不得不这样做,并且不能改变JSON的原始格式

例如

{ "data" : { "username":"Brad", "userID":941022167561310208, "location":"London" } } 

这是正则表达式来包装带引号的所有整数。 如果您只想为长字符串执行此操作,请将+更改为{9,} (或任何int)。

 var json = ('{"data":{"username":"Brad","userID":941022167561310208,"location":"London","test":"3908349804","test2":"This already exists in 034093049034 quotes"},"list":[3409823408,3409823408,"A list 234908234"]}'); json = json.replace(/([\[:])?(\d+)([,\}\]])/g, "$1\"$2\"$3"); json = JSON.parse(json); 

看例子: http : //jsfiddle.net/remus/6YMYT/

编辑更新以容纳列表。

以下是正在使用的正则表达式: http : //regex101.com/r/qJ3mD2

如果你需要把它作为string使用,那么把它保存为一个字符串,用""

 { "data" : { "username":"Brad", "userID":"941022167561310208", "location":"London" } } 

你也有错误的username => "username"

您也可以稍后使用parseInt()其解析为整数

对于更新的问题:那么你需要用正则表达式来包围你的数字:

 s='{"data":{"username":"Brad","userID":9410221675613105435234324235235235235235523208,"location":"London"}}'; s = s.replace(new RegExp('"userID":([0-9]+),',"g"),'"userID":"$1",') 

例如: http : //jsfiddle.net/E4txx/