开发者

json.stringify returns blank [ ] for one object and not a filled { }

I have looked at this post: Jquery - JSON.stringify, array is empty and also checked around with other forums and have searched google and have not found a decent answer to this.

Basically, when I use json.stringify on me saved[] array, it's creating the json string correctly on everything but one array value, which would be at saved[2]. Just for an example here is how the stringify posts both 1 and 2 in the array:

"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/d开发者_JS百科efault/files/neck/pearl_necklace_solid.swf","neck_optlayer":"sites/default/files/neck/pearl_necklace_color.swf","neck_optcolor":"f8e5be"},
"2":[]

It writes out one correctly but when it comes to 2 the values aren't being correctly parsed into a json string. So you know what the saved array looks like at both 1 and 2 value, here (the array is larger, I just think this should be all you need to see what's going wrong. If not, let me know. It works perfect with 1, and not 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"

My question is why is saved[2] being removed from saved when I use: postData = JSON.stringify( saved );

BUT the saved array still exist when it shows 2. Any help would be great full and if you need more insight I will give it to you. The reason I'm using a global variable is because I'm using about 10 different functions that need to call that variable and I don't want to lose sight of it through the designer.

UPDATE: Just realized this issue only happens with firefox too. I'm running firefox 4 Beta 7. Any chance that could be a reason why json.stringify isn't working correctly? Irt's working with Chrome and IE 8.

UPDATE 2: Code For entire save:

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);
        }
    });

}


Are you using an older version of Prototype? I've had a similar issue where Prototype (or another library) adds incompatible .toJSON methods that break stringify.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜