'Uncaught SyntaxError: Unexpected token x in JSON an position 1 ist', wenn Sie versuchen zu analysieren, HTML eingebettete JSON-string

Ive wird versucht zu analysieren, eine fest codierte JSON-string zu einem verwendbaren format, aber ich kann nicht finden, einen Weg, um es Arbeit, immer "Unerwartetes token' oder einfach nur komisch-Ausgänge. Bibliothek: jQuery. Die Daten werden gespeichert, ist:

x:10
y,10

und

x:200
y:10

Es ist gespeichert in einem data Attribut namens data-pos, und ich habe versucht, mehrere Lösungen, die nachstehend aufgeführt:

  • Token-Fehler (alle Kombinationen):

Verwendete Funktionen

JSON.parse(data);
$.parseJSON(data);
jQuery.parseJSON(data);

Datenformate

data-pos="{'x':10,'y':10}"
data-pos="{'x':'10','y':'10}"
data-pos="{x:'10',y:'10'}"
data-pos="{x:10,y:10}"
  • Nur komisch Ausgang (no parsing):

Verwendete Funktionen

alert(data);
    --> output: 
        !!DATA AS IS!!
        !!DATA AS IS!!

alert ('x: ' + data.x + ' y: ' + data.y); /* Using {x: 200, y: 10} format */
    --> outputs:
        x: undefined y: undefined
        x: {y'    <-- Yep, THIS. No typos. IDK what the hell is going on.

Datenformate

data-pos="{'x':10,'y':10}"
data-pos="{'x':'10','y':'10}"
data-pos="{x:'10',y:'10'}"
data-pos="{x:10,y:10}"

Sourcefile:

<html>    
    <head>
        <script>var devmode = true</script>
        <script>var loadExec=false</script>
        <script src="/files/js/jquery.min.js"></script>
    </head>
    <body id="body" class="selectable nooverflow">
        <div class="container hidden desktop">
            <div class="window selectable"  data-pos=!!DATA!!>
                <div class="titlebar">
                    <span class="titletext" onmousedown="movingW=true" onmouseup="movingW=false">Window 1</span>
                </div>
            </div>
            <div class="window" data-pos=!!DATA!!> 
                <div class="titlebar">
                    <span class="titletext" onmousedown="movingW=true" onmouseup="movingW=false">Window 2</span>
                </div>
            </div>
        </div>
        <link rel="stylesheet" type="text/css" href="/files/css/style.css">
        <script src="/files/js/core.js"></script>       <!-- irrelevant -->
        <script src="/files/js/subcore.js"></script>    <!-- irrelevant -->
        <script>        
            $(function () {
                $(window).on("load", function () {
                    $('.window').each(function (index) {
                        if (!loadExec) {
                            var data = $(this).data('pos');
                            //alert(data + "\nDatatype: " + (typeof data));
                            if (data) {
                                alert(data);

                                //PARSING HERE!! <----------

                                $(this).css({ 'top': data.y, 'left': data.x, position: 'absolute' });
                            }

                            loadExec = true;
                            $('.container').removeClass('hidden');
                        }
                    });
                });
            });
        </script>
    </body>
</html>

Habe ich schon gesucht, aber alle die ich finden konnte unsuccesfull war oder ajax ausgerichtet. Ich brauche nur das einbetten dieses JSON-string in einem Attribut oder einige Insider-DOM Weg. Ich kann einfach nicht die Lösung finden.

VIELEN DANK FÜR IHRE ZEIT!

InformationsquelleAutor Akryllax | 2016-05-27
Schreibe einen Kommentar