My goal was to create a utility function that can convert custom data (the type of data Sencha Touch stores use) into JSON format. I've made progress, but the function fails when dealing with complex data from the Twitter API, although it works fine with simpler data types.
Custom Data
var items = [];
for (var i = 0; i < 10; i++) {
var item = {};
var data = {};
data.prop1 = "123456789";
data.prop2 = "Some Name";
data.prop3 = "Some Date and Time";
item.data = data;
items.push(item);
}
The above data can be iterated through in a loop and converted to JSON using the following function.
function toJSON(items) {
var jsonString = "[";
for (var i = 0; i < items.length; i++) {
var item = items[i];
jsonString += "{";
for (var propertyName in item.data) {
jsonString += '"' + propertyName + '":' + '"' + item.data[propertyName] + '",';
}
if (jsonString.substr(jsonString.length - 1, 1) === ",") {
jsonString = jsonString.substr(0, jsonString.length - 1);
}
jsonString += "},";
}
if (jsonString.substr(jsonString.length - 1, 1) === ",") {
jsonString = jsonString.substr(0, jsonString.length - 1);
}
jsonString += "]";
return jsonString;
}
The main question is whether my encoding process is correct?
You can visit these fiddles for a hands-on experience: http://jsfiddle.net/WUMTF/ and