Permalink
Browse files

Add support for JSON

In addition to the MySQL and XML code, it now lets the user generate
JSON code too.
  • Loading branch information...
1 parent 1f699a2 commit 98a5388c07e270bddc257f63f383ff7534065dfe @abhishekbhardwaj abhishekbhardwaj committed Mar 27, 2013
Showing with 34 additions and 1 deletion.
  1. +1 −1 README.md
  2. +4 −0 index.html
  3. +29 −0 js/functions.js
View
@@ -1,6 +1,6 @@
GetCountries
============
-This script will generate MySQL "Countries" table depending on what fields you select. All data is fetched from geonames.org
+This script generates MySQL, XML or JSON code for "Country" data depending on what fields you select. All data is fetched from geonames.org
Demo: http://peric.github.com/GetCountries
View
@@ -106,6 +106,10 @@
<input type="radio" class="codetype" name="codetype" id="xmltype" value="xmltype">
XML
</label>
+ <label class="radio">
+ <input type="radio" class="codetype" name="codetype" id="jsontype" value="jsontype">
+ JSON
+ </label>
<p>
<button id="getcode" class="btn btn-primary" type="button">Get it!</button>
</p>
View
@@ -83,6 +83,7 @@ $(document).ready(function () {
var valuesLength = allValues.length;
var sql = "";
var xml = "";
+ var json = "";
if (settings.type === "mysqltype") {
// create table
@@ -133,6 +134,34 @@ $(document).ready(function () {
// set xml code
$('#generatedcode').text(xml);
+ } else if (settings.type === "jsontype") {
+ json += "{";
+ json += "\n\tcountries: {";
+ json += "\n\t\tcountry: [";
+ for(var i = 0; i < valuesLength; i++) {
+ json += "\n\t\t\t{";
+ for (var j = 0; j < oLength; j++) {
+ var currOption = options[j];
+ var currValue = allValues[i][options[j]];
+
+ if (j == (oLength - 1)) {
+ json += "\n\t\t\t\t" + currOption + ": '" + currValue + "'";
+ } else {
+ json += "\n\t\t\t\t" + currOption + ": '" + currValue + "',";
+ }
+ }
+ if (i == (valuesLength - 1)) {
+ json += "\n\t\t\t}";
+ } else {
+ json += "\n\t\t\t},";
+ }
+ }
+ json += "\n\t\t]";
+ json += "\n\t}";
+ json += "\n}";
+
+ //set json code
+ $('#generatedcode').text(json);
}
}
});

0 comments on commit 98a5388

Please sign in to comment.