Note that there are some explanatory texts on larger screens.

plurals
  1. POCan use this as a json object? Custom string to json parser
    primarykey
    data
    text
    <p>I'm making this little parser that essentially makes json objects out of easier to input strings. I have two input fields as you'll see and I use the first for the table name, then the next as the data page. I'm starting extremely simple so I know this won't work for complex examples. That'll come later. I see i'm missing some {} but I'll add those later. But basically my little parser spits out:</p> <pre><code>{pizzas:[ size:"large";toppings:"sausage"; ] } </code></pre> <p>But now I'm curious, this is technically a working json object right? The var "object" I mean.</p> <p>Here's el code!</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; Input like:&lt;br/&gt; table: pizzas&lt;br/&gt; data: size:"large";toppings:"sausage";&lt;br/&gt; &lt;form name="form1" onsubmit="getString(); return false;"&gt; table name&lt;input type="text" name="tableJob" id="tableJob" value='example: pizzas'&gt;&lt;br/&gt; data&lt;input type="text" name="dataJob" id="dataJob" value='example: size:"large"; toppings:"sausage";'&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt; &lt;p id="myJson"&gt;&lt;/p&gt; &lt;script&gt; var w, x, y, z; function getString(){ x = document.getElementById("tableJob").value; //get the table from user y = document.getElementById("dataJob").value; //get data from user var object = '&lt;p&gt;{' + x + ':[&lt;br/&gt;' + '&amp;nbsp&amp;nbsp' + y + '&amp;nbsp]&lt;br/&gt;' + '}'; document.getElementById("myJson").innerHTML = object; } //now add object to our json page &lt;/script&gt; &lt;/body&gt; </code></pre> <p>//////////////////////////////////////////////////////////////////////////////// This is the correct way now:</p> <pre><code>&lt;body&gt; Input like:&lt;br/&gt; table: "pizzas"&lt;br/&gt; data: "size":"large","toppings":"sausage",&lt;br/&gt; &lt;form name="form1" onsubmit="getString(); return false;"&gt; table name&lt;input type="text" name="tableJob" id="tableJob" value='"pizzas"'&gt;&lt;br/&gt; data&lt;input type="text" name="dataJob" id="dataJob" value='"size":"large", "toppings":"sausage,"'&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt; &lt;p id="myJson"&gt;&lt;/p&gt; &lt;script&gt; var w, x, y, z; function getString(){ x = document.getElementById("tableJob").value; //get the table from user y = document.getElementById("dataJob").value; //get data from user var obj = '&lt;p&gt;{&amp;nbsp' + x + '&amp;nbsp:&amp;nbsp[&lt;br/&gt;' + '&amp;nbsp&amp;nbsp{' + y + '}&amp;nbsp]&lt;br/&gt;' + '}'; document.getElementById("myJson").innerHTML = obj; var myJsonString = '{' + x + ':[' + '{' + y + '} ]' + '}'; alert(myJsonString); } &lt;/script&gt; &lt;/body&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload