angularjs - Removing excess comma on JSON Object -


currently been working on eliminating excess "," comma on json object have below.

{"rules": {     "1000": {         "action": "2",         "category": "skype",         "entity": "private",         "id": "1000",     },     "1200": {         "action": "2",         "category": "http",         "entity": "public",         "id": "1200",     },     "100": {         "action": "2",         "category": "ftp",         "entity": "public",         "id": "100",     },     "0": {         "entity": "private",         "category": "alcohol, tobacco",         "action": "1",         "id": "low",     },     "3000": {     } }} 

maybe have insights on what's cleanest way eliminate using angularjs.

the data parsed code snippet.

var request = {                 url: 'sample/uri',                 method: "get",                 transformresponse: specialtransform             };               var response = $q.defer( );                 $http( request ).success( function( data -> data, status ) { 

  1. eval

    var fixtrailingcommas = function (jsonstring) {     var jsonobj;     eval('jsonobj = ' + jsonstring);     return json.stringify(jsonobj); };  fixtrailingcommas('{"rules": { "1000": { "action": "2", "category": "skype", "entity": "private", "id": "1000" ,   } } }'); 

    please use eval here if trust incoming json, , aware of other eval evils described on mdn , note on json parsing

    note since json syntax limited compared javascript syntax, many valid javascript literals not parse json. example, trailing commas not allowed in json, , property names (keys) in object literals must enclosed in quotes. sure use json serializer generate strings later parsed json.

  2. you may choose rely on implementation of json2 douglas crockford uses eval internally

    on current browsers, file nothing, preferring built-in json object. there no reason use file unless fate compels support ie8, no 1 should ever have again.

    but because need use library, have make few code modifications, e.g. comment out json type check, override native browser object (or may introduce new json2 global variable)

    //if (typeof json !== 'object') {     json = {}; //} 

    p.s. other parsing fuctions json_parse.js , json_parse_state.js, don't use eval, throw syntax error

  3. angular part

    var config = {     transformresponse: function (data, headers) {         if(headers("content-type") === "application/json" && angular.isstring(data)) {              try {                 data = json.parse(data);             } catch (e) {                 // if parsing error, try parser                 // or fix commas, if know sure problem in commas                 data = json2.parse(data);             }              return data;          } else {             return data;         }     } };  $http.get("rules.json", config).success(function (data) {     $scope.rules = data; }); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -