正如标题所暗示的那样,我正试图JSON.stringify在我的Node.js应用程序中使用巨大的JavaScript对象进行字符串化.对象 - 再次 - 巨大(几十兆字节),它们不包含任何函数.我需要将序列化对象写入文件.我现在得到的是:
RangeError: Invalid string length
at Object.stringify (native)
at stringifyResult (/my/file.js:123:45) -> line where I use JSON.stringify
Run Code Online (Sandbox Code Playgroud)
知道如何解决这个问题吗?
我想给出一个简单的true响应,但根据各种 JSON 解析器,这不是有效的JSON:
true
Run Code Online (Sandbox Code Playgroud)
但是,true在编码和解码时,PHP和Javascript的行为类似于"true"确实是有效的JSON :
PHP的
echo json_encode( true ); // outputs: true
echo json_decode( true ); // outputs: 1
echo gettype(json_decode( true )); // outputs: boolean
Run Code Online (Sandbox Code Playgroud)
jQuery-
JSON.stringify( true ); // outputs: true
jQuery.parseJSON( true ); // outputs: true
typeof jQuery.parseJSON( true ); // outputs: boolean
Run Code Online (Sandbox Code Playgroud)
那么发送true格式为JSON 的响应的正确方法是什么?验证人都错了吗?
在我编写的歌词应用程序中,我使用数组来打印艺术家表.艺术家阵列看起来像这样:
$artists = [
[ "Avril Lavigne" ],
[ "3 Doors Down" ],
[ "Celine Dion" ],
[ "Evanescence" ],
[ "Shania Twain" ],
[ "Green Day" ],
//...
];
Run Code Online (Sandbox Code Playgroud)
在打印之前,我对阵列进行了一些修改.我有一个包含歌词文件的每个艺术家的文件夹.我将文件夹名称添加到$artists数组中供以后使用:
$folder_fix = [
[" ", "_" ],
[".", "" ],
["&", "n" ],
];
for ($i = 0; $i < count($artists); $i++) {
$folder_name = strtolower($artists[$i][0]);
for ($k = 0; $k < count($folder_fix); $k++) {
$folder_name = str_replace($folder_fix[$k][0], $folder_fix[$k][1], $folder_name);
}
array_push($artists[$i], $folder_name);
}
Run Code Online (Sandbox Code Playgroud)
之后,我将每位艺术家的专辑和曲目数添加到数组中:
$lyrics_base = …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种解决方案,将Javascript对象序列化(和反序列化)到跨浏览器的字符串,包括碰巧是函数的对象的成员.典型的对象如下所示:
{
color: 'red',
doSomething: function (arg) {
alert('Do someting called with ' + arg);
}
}
Run Code Online (Sandbox Code Playgroud)
doSomething()只包含局部变量(不需要序列化调用上下文!).
JSON.stringify()将忽略'doSomething'成员,因为它是一个函数.我知道toSource()方法会做我想要的,但它是FF特定的.
我有一个JavaScript ES6类,它具有一个属性集,set并通过get函数进行访问.它也是一个构造函数参数,因此可以使用所述属性实例化该类.
class MyClass {
constructor(property) {
this.property = property
}
set property(prop) {
// Some validation etc.
this._property = prop
}
get property() {
return this._property
}
}
Run Code Online (Sandbox Code Playgroud)
我_property用来逃避使用get/set的JS问题,如果我直接设置它会导致无限循环property.
现在我需要对MyClass的一个实例进行字符串化,以便通过HTTP请求发送它.字符串化的JSON是一个对象,如:
{
//...
_property:
}
Run Code Online (Sandbox Code Playgroud)
我需要保留生成的JSON字符串,property以便我发送给它的服务可以正确解析它.我还需要property保留在构造函数中,因为我需要从服务发送的JSON构造MyClass的实例(它发送property没有的对象_property).
我该如何解决这个问题?如果我只是把它发送到HTTP请求之前截获MyClass的实例,并发生变异_property,以property使用正则表达式?这看起来很难看,但我能够保留当前的代码.
或者,我可以拦截从服务发送到客户端的JSON,并使用完全不同的属性名称实例化MyClass.但是,这意味着服务两侧的类的不同表示.
内存使用在我的应用程序中非常重要.因此,我有特定的断言,在编译时检查内存大小,如果大小与我们之前认为正确的大小不同,则给出static_assert.
我已经定义了一个像这样的宏:
#define CHECKMEM(mytype, size) static_assert((sizeof(objectType) == size)), "Size incorrect for " #mytype "!");
Run Code Online (Sandbox Code Playgroud)
这个宏使得编写它非常容易:
CHECKMEM(Book,144);
CHECKMEM(Library,80);
Run Code Online (Sandbox Code Playgroud)
问题是当这个static_assert出现时,可能很难找出新的大小应该是什么(例如通过使用隐藏的编译器选项"/ d1 reportAllClassLayout").如果我可以包含实际大小会更方便,所以代替:
Book的大小不正确!
它会显示出来
Book的大小不正确!(预计144,大小为152)
我试着写这样的东西:
#define CHECKMEM(mytype, size) static_assert((sizeof(objectType) == size)), "Size incorrect for " #mytype "! (expected" #size ", size is " #sizeof(mytype) ")");
Run Code Online (Sandbox Code Playgroud)
但是你不能在函数调用中使用stringize(#)运算符.
我也尝试添加双串联技巧,如下所示:
#define STR1(x) #x
#define STR2(x) STR1(x)
#define CHECKMEM(mytype, size) static_assert((sizeof(objectType) == size)), "Size incorrect for " #mytype "! (expected" #size ", size is " STR2(sizeof(mytype)) ")");
Run Code Online (Sandbox Code Playgroud)
但不是打印size is 152它打印size is sizeof(Book) …
我需要一些有关此错误的帮助:
未捕获的 SyntaxError:在 Object.success (dashboard.js:22) at fire (jquery-3.3.1.js:3268) at Object.fireWith [as resolveWith] (jquery-3.3) 处的 JSON.parse () 处的 JSON 输入意外结束.1.js:3398) 在完成 (jquery-3.3.1.js:9305) 在 XMLHttpRequest。(jquery-3.3.1.js:9548)
我尝试使用导致该错误的JSON.parse()将字符串转换为 json 对象。我正在使用 oracleJet,这是我的代码:
function DashboardViewModel() {
var self = this;
self.lineTypeValue = ko.observable('curved');
var scatterSeries = [];
$.getJSON( "http://localhost:8080/points", function (data) {
console.info(data);
var ch = '{"name":"graphe1","items":'+JSON.stringify(data.results[1])+ '}';
console.info(ch);
console.info(JSON.parse(scatterSeries));
scatterSeries.push(JSON.parse(ch));
});
/* chart data */
this.scatterSeriesValue = ko.observableArray(scatterSeries);
self.lineTypeOptions = [
{id: 'straight', label: 'straight'},
{id: 'curved', label: 'curved'},
{id: 'stepped', label: 'stepped'}, …Run Code Online (Sandbox Code Playgroud) 在我的代码中,当选择特定的rowID时,Postgres表行中的所有信息都会被字符串化.
var jsonRes = result.message.rows;
document.getElementById('panel').innerHTML = '<pre>' + JSON.stringify(jsonRes[0], null, "\t") + '</pre>'
Run Code Online (Sandbox Code Playgroud)
结果看起来像这样:
{
"ogc_fid": 143667,
"relkey": 288007,
"acct": "000487000A0010000",
"recacs": "12.5495 AC",
"shape_star": 547131.567383,
"shape_stle": 3518.469618,
"objectid": 307755,
"zone_dist": "MU-3",
"pd_num": null,
"council_da": null,
"long_zone_": "MU-3",
"globalid": "{D5B006E8-716A-421F-A78A-2D71ED1DC118}",
"ord_num": null,
"notes": null,
"res_num": null,
"effectived": 1345766400000,
"shape.star": 629707.919922,
"shape.stle": 3917.657332,
"case_numbe": null,
"common_nam": null,
"districtus": null
}
Run Code Online (Sandbox Code Playgroud)
我是JS的新手,想知道是否有一种简单的方法可以完全排除包含空值的列 - 这个函数大致如下所示:
function hide(jsonObject) {
if (property === null) {
hide property
} else {
return str
}
} …Run Code Online (Sandbox Code Playgroud) 我正在开发一款游戏,我们为数学类型广泛使用了类型数组(Float32Arrays).我们从JSON保存并加载游戏状态.JSON stringify输出的一个例子就是这样一个数组(在Chrome中)是:
"{"0":0,"1":0,"2":0,"length":3,"byteLength":12,"byteOffset":0,"buffer":{"byteLength":12}}"
Run Code Online (Sandbox Code Playgroud)
这会浪费空间并导致它们作为不方便的对象被加载.理想情况下,我们可以使用stringify'replacer'函数来测试变量是否是类型化数组,然后在这种情况下将其转换为bog标准数组.不幸的是,我不确定如何可靠地测试变量是否是类型化数组.
有帮助吗?
有没有办法JSON.stringify在Android中执行?
我一直JSON.stringify(JSONObject)在网上看到,但我无法在android中找到JSON类.
有帮助吗?
stringify ×10
json ×7
javascript ×6
android ×1
arrays ×1
c++ ×1
c++11 ×1
ecmascript-6 ×1
es6-class ×1
function ×1
java ×1
node.js ×1
oracle-jet ×1
parsing ×1
php ×1
typedarray ×1