Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将字符串转换为有效的json对象_Javascript_Jquery_Json_Forms_Serializearray - Fatal编程技术网

Javascript 将字符串转换为有效的json对象

Javascript 将字符串转换为有效的json对象,javascript,jquery,json,forms,serializearray,Javascript,Jquery,Json,Forms,Serializearray,我试图从包含表单值的字符串返回一个有效的JSON对象,但是,使用JavaScript JSON.parse返回一个char值数组,而不是JSON对象。下面是我的代码: <!DOCTYPE html> <html> <head> <title>Select Browser Options</title> <meta http-equiv="Content-Type" content="text/html; c

我试图从包含表单值的字符串返回一个有效的JSON对象,但是,使用JavaScript JSON.parse返回一个char值数组,而不是JSON对象。下面是我的代码:

<!DOCTYPE html>
<html>
    <head>
    <title>Select Browser Options</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src='https://code.jquery.com/jquery-2.0.3.js'></script>
    </head>
    <body style="width: 640px;height: 480px;">
    <div id="hiddenX" style="display:none;"></div>
    <div id="hiddenY" style="display:none;"></div>
    <div id="searchPrefs">
        <form id="searchPreferences">
        <div id="searchWrapper">
            <input class="id" type="text" name="id" value="googleSearch">
            <input class="label" type="text" name="label" value="Google Search">
            <input class="iconURL" type="text" name="iconURL" value="https://www.google.com/favicon.ico">
            <input class="tabURL" type="text" name="tabURL" value="https://www.google.com/search?q=">
            <br></br>
            <input class="id" type="text" name="id" value="googleMapSearch">
            <input class="label" type="text" name="label" value="Google Maps">
            <input class="iconURL" type="text" name="iconURL" value="https://maps.gstatic.com/favicon3.ico">
            <input class="tabURL" type="text" name="tabURL" value="https://www.google.com/maps/preview/search/">
            <br></br>
            <input class="id" type="text" name="id" value="ddgSearch">
            <input class="label" type="text" name="label" value="Duck Duck Go Search">
            <input class="iconURL" type="text" name="iconURL" value="https://duckduckgo.com/favicon.ico">
            <input class="tabURL" type="text" name="tabURL" value="https://duckduckgo.com/?q=">
            <br></br>
            <input class="id" type="text" name="id" value="imageSearch">
            <input class="label" type="text" name="label" value="Google Image Search">
            <input class="iconURL" type="text" name="iconURL" value="data:image/gif;base64,R0lGODlh…GbsuF0PHJq9WipnYJB9/UmFyIAOw==">
            <input class="tabURL" type="text" name="tabURL" value="https://www.google.com/search?tbm=isch&q=">
            <br></br>
            <input class="id" type="text" name="id" value="bingSearch">
            <input class="label" type="text" name="label" value="Bing Search">
            <input class="iconURL" type="text" name="iconURL" value="http://bing.com/favicon.ico">
            <input class="tabURL" type="text" name="tabURL" value="http://www.bing.com/search?q=">
            <br></br>
            <input class="id" type="text" name="id" value="thesaurusSearch">
            <input class="label" type="text" name="label" value="Thesaurus Search">
            <input class="iconURL" type="text" name="iconURL" value="http://static.sfdict.com/thescloud/favicon.ico">
            <input class="tabURL" type="text" name="tabURL" value="http://thesaurus.com/browse/">
            <br></br>
            <input class="id" type="text" name="id" value="wikipediaSearch">
            <input class="label" type="text" name="label" value="Wikipedia Search">
            <input class="iconURL" type="text" name="iconURL" value="https://bits.wikimedia.org/favicon/wikipedia.ico">
            <input class="tabURL" type="text" name="tabURL" value="https://en.wikipedia.org/w/index.php?search=">
            <br></br>
            <input class="id" type="text" name="id" value="wiktionarySearch">
            <input class="label" type="text" name="label" value="Wiktionary Search">
            <input class="iconURL" type="text" name="iconURL" value="https://bits.wikimedia.org/favicon/wiktionary/en.ico">
            <input class="tabURL" type="text" name="tabURL" value="https://en.wiktionary.org/w/index.php?search=">
            <br></br>
        </div>
        <a id="more" href="#"></a>
        <br></br>
        <input id="save" type="submit" value="save" name="save">
        </form>
        <div id="output"></div>
        <script>
        $('#searchPreferences').submit(function(event) {
            var justValues = "[";
            var formJson = $('#searchPreferences').serializeArray();
            var lastKey = $("input").length - 2;
            $.each(formJson, function(i, field) {
            if (field.name === "id") {
                justValues += '{"' + field.name + '":"' + field.value + '",';
            } else if (field.name === "tabURL" && i === lastKey) {
                justValues += '"' + field.name + '":"' + field.value + '"}';
            } else if (field.name === "tabURL") {
                justValues += '"' + field.name + '":"' + field.value + '"},';
            } else {
                justValues += '"' + field.name + '":"' + field.value + '",';
            }
            });
            justValues += "]";
            JSON.parse(justValues);
            for(var o in justValues){
            console.log(o+':'+justValues[o]);
            }
            console.log(justValues);
            event.preventDefault();
        });
        </script>
    </div>
    </body>
</html>

看起来您没有对JSON.parse的返回值做任何操作。我想你想做的是

justValues = JSON.parse(justValues);

这应该能让它工作。然而,这是一种间接的方式来获得你想要的东西。如果您想要的是JavaScript对象,为什么不直接构建该对象呢?而不是构建一个JSON字符串,然后将其解析为一个对象。

当然可能是Duh的重复!我花了好几个小时绞尽脑汁,想知道为什么它没有返回我想要的东西,我只是需要一双新的眼睛。谢谢
justValues = JSON.parse(justValues);