Skip to content

Commit

Permalink
Merge pull request #4 from abhishekbhardwaj/master
Browse files Browse the repository at this point in the history
Adds support for JSON.
  • Loading branch information
peric committed Mar 27, 2013
2 parents 1f699a2 + 98a5388 commit b61901e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
@@ -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
4 changes: 4 additions & 0 deletions index.html
Expand Up @@ -106,6 +106,10 @@ <h5>Select type:</h5>
<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>
Expand Down
29 changes: 29 additions & 0 deletions js/functions.js
Expand Up @@ -83,6 +83,7 @@ $(document).ready(function () {
var valuesLength = allValues.length;
var sql = "";
var xml = "";
var json = "";

if (settings.type === "mysqltype") {
// create table
Expand Down Expand Up @@ -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 b61901e

Please sign in to comment.