Question

I am trying to return a valid JSON object from a string which contains the values from a form, however, using JavaScript JSON.parse returns an array of char values and not a JSON object. Below is my code:

<!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>

and here is the console.log output:

...
1210:?
1211:s
1212:e
1213:a
1214:r
1215:c
1216:h
1217:=
1218:"
1219:}
1220:]
[{"id":"googleSearch","label":"Google Search","iconURL":"https://www.google.com/favicon.ico","tabURL":"https://www.google.com/search?q="},{"id":"googleMapSearch","label":"Google Maps","iconURL":"https://maps.gstatic.com/favicon3.ico","tabURL":"https://www.google.com/maps/preview/search/"},{"id":"ddgSearch","label":"Duck Duck Go Search","iconURL":"https://duckduckgo.com/favicon.ico","tabURL":"https://duckduckgo.com/?q="},{"id":"imageSearch","label":"Google Image Search","iconURL":"data:image/gif;base64,R0lGODlh…GbsuF0PHJq9WipnYJB9/UmFyIAOw==","tabURL":"https://www.google.com/search?tbm=isch&q="},{"id":"bingSearch","label":"Bing Search","iconURL":"http://bing.com/favicon.ico","tabURL":"http://www.bing.com/search?q="},{"id":"thesaurusSearch","label":"Thesaurus Search","iconURL":"http://static.sfdict.com/thescloud/favicon.ico","tabURL":"http://thesaurus.com/browse/"},{"id":"wikipediaSearch","label":"Wikipedia Search","iconURL":"https://bits.wikimedia.org/favicon/wikipedia.ico","tabURL":"https://en.wikipedia.org/w/index.php?search="},{"id":"wiktionarySearch","label":"Wiktionary Search","iconURL":"https://bits.wikimedia.org/favicon/wiktionary/en.ico","tabURL":"https://en.wiktionary.org/w/index.php?search="}]
Was it helpful?

Solution

Looks like you aren't doing anything with the return value of JSON.parse. I think what you want to do is.

justValues = JSON.parse(justValues);

That should get it working. However, this is an indirect way of getting what you're after. If what you want is a JavaScript object, why not just build that object directly? Rather than building a JSON string and then parsing it into an object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top