JSON.parse:JSON数据之后的意外非空白字符

Mik*_*Sav 8 javascript jquery json

我希望创建一个JSON对象,该对象派生自4个选择菜单的选定选项.这些菜单可能在加载时选择了选项(由于服务器端技术),或者可能没有选择任何选项!一旦使用$(document).ready()加载页面,我的脚本就会运行...但是我遇到了JSON对象的一些问题" JSON.parse:JSON数据后出现意外的非空白字符 "

我希望我的JSON具有以下结构

selObj = {

    category: selectedCategory, // we can only have 1 category, this isn’t giving me a problem…

    catOptions:{
        optionValue:discountValue, // we can have many of these
        optionValue:discountValue,
        optionValue:discountValue
    },

    tariff:{
        tariffValue:discountValue, // we can have many of these
        tariffValue:discountValue
    },

    tarOptions:{
        tarOption:discountValue, // we can have many of these

    }

};
Run Code Online (Sandbox Code Playgroud)

好的,这是我的函数,我已经删除了一些项目以简化,但我将原始的空对象作为参数传递给函数,然后希望修改它,因为我想将JSON对象传递回服务器,如果/当选择菜单被更改时...但是现在我们只是处理页面加载...该函数还显示了动态表中使用的函数创建的值或选择的内容,defaultTable()只是忽略现在,它是填充我感兴趣的JSON对象(并且遇到问题).

var selectMenuArray = ["cat", "catOpt", "tariff", "tariffOpt"], // these are the IDs of the select menus...     
selObj = {};

function setUpTable(selectArray, theObj){

    // okay, if we enter the page and and the user already has a tarif selected (so it is shown in the menus) we wish to show the table with the 
    // default/original values...

    var currentSelect,
        catA = [],
        catOptA = [],
        tarA = [],
        tarOptA  = [],
        catOptForObj,
        tariffForObj,
        tariffOptForObj,
        temp1 = {},
        temp2 = {},
        temp3 = {},
        i;

    // loop through the 4 menus and see what is selected
    for(i=0;i<selectArray.length;i++){
    // let's look at the options to see if any are selected by looping through the <option>
        currentSelect = document.getElementById(selectArray[i]);

        for(j=0;j<currentSelect.length;j++){
            // do we have an options with the selected attribute?
            if(currentSelect.options[j].selected == true){
                // okay, we need to make sure the table is shown then the correct values are shown?
                // we know what the Menu is... selectArray[i], and this is the text... currentSelect.options[j].
                // lets build up whet's selected in Arrays...
                switch(selectArray[i]){
                    case "cat":
                        catA.push(currentSelect.options[j].value);
                        break;
                    case "catOpt":          
                        catOptA.push(currentSelect.options[j].value);
                        catOptForObj = catOptForObj + '"' + currentSelect.options[j].value + '":"' + "% to come later" + '",';
                        break;
                    case "tariff":
                        tarA.push(currentSelect.options[j].value);
                        tariffForObj = tariffForObj + '"' + currentSelect.options[j].value + '":"' + "% to come later" + '",';
                        break;  
                    case "tariffOpt":
                        tarOptA.push(currentSelect.options[j].value);
                        tariffOptForObj = tariffOptForObj + '"' + currentSelect.options[j].value + '":"' + "% to come later" + '",';
                        break;  
                    default:
                    // no default?
                }
            }
        }
    }

    // now we can build the table
    if(catA.length > 0 || catOptA.length > 0 || tarA.length > 0 || tarOptA.length > 0){

        // show table...
        $("#dynamicTableHolder table").css("visibility","visible").hide().fadeIn('slow');

        for(i=0;i<selectArray.length;i++){  
            switch(selectArray[i]){ 
                case "cat":
                    defaultTable(catA, "dtCats");
                    theObj.cat = catA[0];
                    break;
                case "catOpt":          
                    defaultTable(catOptA, "dtTariffs");
                    temp1 = '"{' + catOptForObj.substring(0, catOptForObj.length-1).replace(/undefined/g, "")  + '}"'; 
                    theObj = jQuery.parseJSON('"catOptions":' + temp1);
                    break;
                case "tariff":
                    defaultTable(tarA, "dtCatOpts");
                    temp2 = "{" + tariffForObj.substring(0, tariffForObj.length-1).replace(/undefined/g, "") + "}";
                    //theObj = jQuery.parseJSON('"tariff":' + temp2);
                    break;  
                case "tariffOpt":
                    defaultTable(tarOptA, "dtTariffOpts");
                    temp3 = "{" + tariffOptForObj.substring(0, tariffOptForObj.length-1).replace(/undefined/g, "") + "}";
                    //theObj = jQuery.parseJSON('"tarOptions":' + temp3);
                    break;  
                default:
                // no default?
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我在制作JSON对象时遇到问题,我正在尝试做的是在循环时连接字符串,然后使用jQuery.parseJSON方法将这些字符串转换为对象...但是我没有正确地执行此操作.我甚至尝试在创建temp1时更改引号.

我知道这很复杂,我可能会问很多,但任何人都可以看到我做错了什么.我对jQuery.parseJSON并不熟悉,所以任何建议都会很棒,因为我觉得我疯了!

如果我模糊不清或者没有自己解释,请说出来......我也把它放在小提琴上...... http://jsfiddle.net/itakesmack/JSRt7/1/

请注意,这是一个工作进展,因此有些项目可能会丢失或需要开发(或者我可能有其他错误......但我希望不会).

Pet*_*ete 3

您正在向 parseJSON() 提供一个看起来像'"catOptions":"{"A":"B","C":"D",}"' parseJSON() 的字符串,想要看起来像这样的东西 '{"catOptions":{"A":"B","C":"D"}}'

如果您要手动构建 JSON,则需要处理一些问题。

要修复您的错误,您不想将您的{key:value}引号括起来,它们会被 catOptForObj 内的引号破坏

如果构建 OptForObj 字符串时使用的值可能包含双引号,则需要对其进行转义

您基本上是这样构建字符串的:

s = s + '"' + A '":"' + "B" + '",';
Run Code Online (Sandbox Code Playgroud)

这会产生类似的东西"A":"B","C":"D",

逗号只能存在于值之间。一种方法是首先检查 s 是否为空,然后仅将其附加到 s 之前的附加值。就像是:

s = (s.length?(s+','):'') + '"' + A + '":"' + "B" + '"';
Run Code Online (Sandbox Code Playgroud)

最好的方法是依赖内置函数,如评论中建议的 JSON.stringify