Convert a javascript associative array into json object using stringify and vice versa

后端 未结 5 712
深忆病人
深忆病人 2020-12-14 02:42

I have a javascript associative array like one below

 var my_cars= new Array()
 my_cars[\"cool\"]=\"Mustang\";
 my_cars[\"family\"]=\"Station Wagon\";
 my_ca         


        
5条回答
  •  盖世英雄少女心
    2020-12-14 02:58

    If for some reason you cannot convert your array into object, for instance you are working on a big framework or legacy code that you dont want to touch and your job is only to add som feature which requires JSON API use, you should consider using JSON.stringify(json,function(k,v){}) version of the API. In the function you can now decide what to do with value of key is of a specific type.

提交回复
热议问题