gpt4 book ai didi

javascript - json.stringify 对于一个对象返回空白 [ ],而不是填充的 { }

转载 作者:行者123 更新时间:2023-11-28 10:25:39 26 4
gpt4 key购买 nike

我看过这篇文章: Jquery - JSON.stringify, array is empty并且还检查了其他论坛并搜索了谷歌,但没有找到合适的答案。

基本上,当我在 saving[] 数组上使用 json.stringify 时,它会在除一个数组值之外的所有内容上正确创建 json 字符串,该数组值将位于 saving[2] 处。举个例子,这里是 stringify 如何在数组中同时发布 1 和 2:

"1":{"doodleID":"716","tid":"26","outline":"sites/default/files/doodlekin/female24solid.swf","skin":"sites/default/files/doodlekin/female24face.swf","hair":"sites/default/files/doodlekin/female24hair.swf","misc":"sites/default/files/doodlekin/female24lips.swf","addlayer1":"","addlayer2":"","addlayer3":"","doodlename":"Mom","headsize":"95","flip":"true","skincolor":"E8B18C","skinblush":"sites/default/files/skin_color/e8b18c.swf","haircolor":"855D33","mouth":"sites/default/files/mouth/lips_color_d6570a_1.swf","neck":"sites/default/files/neck/pearl_necklace_solid.swf","neck_optlayer":"sites/default/files/neck/pearl_necklace_color.swf","neck_optcolor":"f8e5be"},
"2":[]

它正确地写出了 1,但是当写到 2 时,这些值没有被正确解析为 json 字符串。所以你知道保存的数组在 1 和 2 值下是什么样子,在这里(数组更大,我只是认为这应该是你需要查看发生了什么问题的全部。如果没有,请告诉我。它与 1 完美配合) ,而不是 2) :

  '1' ...
'doodleID' => "716"
'tid' => "26"
'outline' => "sites/default/files/doodlekin/female24solid.swf"
'skin' => "sites/default/files/doodlekin/female24face.swf"
'hair' => "sites/default/files/doodlekin/female24hair.swf"
'misc' => "sites/default/files/doodlekin/female24lips.swf"
'addlayer1' => ""
'addlayer2' => ""
'addlayer3' => ""
'doodlename' => "Mom"
'headsize' => "95"
'flip' => "true"
'skincolor' => "E8B18C"
'skinblush' => "sites/default/files/skin_color/e8b18c.swf"
'haircolor' => "855D33"
'mouth' => "sites/default/files/mouth/lips_color_d6570a_1.swf"
'neck' => "sites/default/files/neck/pearl_necklace_solid.swf"
'neck_optlayer' => "sites/default/files/neck/pearl_necklace_color.swf"
'neck_optcolor' => "f8e5be"
'2' ...
'doodleID' => "291"
'tid' => "27"
'outline' => "sites/default/files/doodlekin/male66solid.swf"
'skin' => "sites/default/files/doodlekin/male66face.swf"
'hair' => "sites/default/files/doodlekin/male66hair.swf"
'misc' => ""
'addlayer1' => ""
'addlayer2' => ""
'addlayer3' => ""
'doodlename' => "John"
'headsize' => "95"
'skincolor' => "EEBF7C"
'skinblush' => "sites/default/files/skin_color/eebf7c.swf"
'haircolor' => "353535"
'eyes' => ""
'eyes_shadow' => ""
'eyes_optlayer' => ""
'eyes_optcolor' => ""
'mouth' => "sites/default/files/mouth/goatee_solid.swf"
'mouth_optlayer' => "sites/default/files/mouth/goatee_color.swf"
'mouth_optcolor' => "646464"

我的问题是,为什么当我使用以下命令时,saved[2] 会从 Saved 中删除:postData = JSON.stringify( 保存 );

但是当显示 2 时,保存的数组仍然存在。任何帮助都会非常完整,如果您需要更多见解,我会给您提供帮助。我使用全局变量的原因是因为我使用了大约 10 个需要调用该变量的不同函数,并且我不想通过设计器忽视它。

更新:刚刚意识到这个问题也只发生在 Firefox 上。我正在运行 firefox 4 Beta 7。是否有可能是 json.stringify 无法正常工作的原因? Irt 可与 Chrome 和 IE 8 配合使用。

更新2:整个保存的代码:

function userSavedDoodlekin(flag)
{
// For leave page check
addToCartPage = "true";

pID = $("#neededInfo .productID").text();
tID = $("#neededInfo .templateID").text();
pAttr = new Object();
tBg = $("#neededInfo .templateBG").text();
tCoords = $("#neededInfo .templateCoords").text();

$("#neededInfo .productAttr").each(function(){
pAttr[$(this).attr('alt')] = $(this).text();
});

store_doodlekin("product", pID, "productID");
store_doodlekin("product", pAttr, "productAttr");
store_doodlekin("product", tID, "templateID");
store_doodlekin("product", tBg, "templateBG");
store_doodlekin("product", tCoords, "templateCoords");

// Data for post "json" serialized
postData = JSON.stringify( saved );
flag = '';

// Check for flag
if(flag == "true")
{
flag = '?saved=true';
}

// Test before launch
// alert(postData);
// alert(dump(saved));

// Save the doodlekin and start another loop for the next one to save if another in line.
// Data Sent Back: object.status = (1 or 0), object.data = (error message)

$.ajax({
type: 'POST',
dataType: 'json',
data: "data="+postData,
url: "/designer/save",
success: function(obj){
if(obj.status == 1){
// alert(obj.data);
window.location = "/cart";
}
else if(obj.status == 0){
alert(obj.data);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert("textStatus: "+textStatus);
alert("errorThrown: "+errorThrown);
}
});

}

最佳答案

您使用的是旧版本的 Prototype 吗?我遇到了类似的问题,Prototype(或其他库)添加了不兼容的 .toJSON 方法,从而破坏了 stringify。

关于javascript - json.stringify 对于一个对象返回空白 [ ],而不是填充的 { },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4401365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com