Thursday, 15 August 2013

Can use this as a json object? Custom string to json parser

Can use this as a json object? Custom string to json parser

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. But basically my little parser spits out:
{pizzas:[
size:"large";toppings:"sausage"; ]
}
But now I'm curious, this is technically a working json object right? The
var "object" I mean.
Here's el code!
<!DOCTYPE html>
<html>
<head>
</head>
<body>
Input like:<br/>
table: pizzas<br/>
data: size:"large";toppings:"sausage";<br/>
<form name="form1" onsubmit="getString(); return false;">
table name<input type="text" name="tableJob" id="tableJob"
value='example: pizzas'><br/>
data<input type="text" name="dataJob" id="dataJob" value='example:
size:"large"; toppings:"sausage";'>
<input type="submit" value="Submit">
</form>
<p id="myJson"></p>
<script>
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 = '<p>{' + x + ':[<br/>' + '&nbsp&nbsp' + y +
'&nbsp]<br/>' + '}';
document.getElementById("myJson").innerHTML = object;
}
//now add object to our json page
</script>
</body>

No comments:

Post a Comment