将JSON格式转换为特定格式

Convert JSON format in specific format

我有 JSON 个对象

countryMap = Object {KE: 10, BR: 9, NP: 7}

现在我想要它的形式像

[
      {
          "code": "KE",
          "value": 10
      },
      {
          "code": "BR",
          "value": 9
      },
      {
          "code": "NP",
          "value": 7
      }

];

我试过的是

$.each(countryMap, function(key, value){
        item = {};
        item ["code"] = key;
        item ["value"] = value;
        totalCountries+=1;
        jsonObj.push(item);
}); 

但我得到的是

    [
      {
          "code": "KE"
          "value": 10
      },
      {
          "code": "BR"
          "value": 9
      },
      {
          "code": "NP"
          "value": 7
      }

];

请注意,代码值后缺少 comma

如何实现我想要的准确格式?

试试这个..

countryMap = {KE: 10, BR: 9, NP: 7};
var jsonObj=[];
$.each(countryMap, function(key, val){
        jsonObj.push({code:key,value:val});
}); 
alert(JSON.stringify(jsonObj));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

你应该试试这个:

$.each(countryMap, function(key, value){
   item = {"code": key, "value": value};
   totalCountries+=1;
   jsonObj.push(item);
});