samedi 14 février 2015

Converting JSON to specific string format


I get the following json data from server.



{"visits":[{"City":6,"Count":5},{"City":16,"Count":1},{"City":23,"Count":1},{"City":34,"Count":1}]}


and i need to convert it to following format:



{"1":"82700","2":"26480","3":"31530","4":"22820","5":"15550","6":"205790"}


I have the following code but not working out:



var cities = "{";

for (var key in data.visits) {
var val = data.visits[key];
//Now you have your key and value which you
//can add to a collection that your plugin uses
var obj = {};
obj[val.City] = '' + val.Count;

var code = '' + val.City;
var count = '' + val.Count;

cities += code + ':' + count + ',';
}

cities += "}";


I need the integers in string representation and need to get rid of the final , .


How can i fix this?





Aucun commentaire:

Enregistrer un commentaire