以所需格式重新格式化json

时间:2016-05-23 07:21:59

标签: javascript jquery json jqgrid

{
    "EUR": {
        "Description": "",
        "Bid": "1.1222",
        "Region": "",
        "Bid1": "1.1283",
        "CurrencyCode": "EUR",
        "FeedSource": "1",
        "Ask": "1.1226",
        "ProviderName": "TEST 1",
        "Low": "1.1195",
        "LastModified": "2014-05-20T08:11:13",
        "CreatedBy": "10000",
        "OpenBid": "1.12",
        "QuotedCurrencyCode": "USD"
    },
    "PHP": {
        "Description": "",
        "Bid": "46.75",
        "Region": "",
        "Bid1": "4.59",
        "CurrencyCode": "PHP",
        "FeedSource": "1",
        "Ask": "46.755",
        "ProviderName": "Test2",
        "Low": "4.715",
        "LastModified": "2016-05-20T07:54:32",
        "CreatedBy": "10000",
        "QuotedCurrencyCode": "USD"
    }
}

需要以下面的格式重新格式化这个json,

{ CurrencyCode: "EUR", LastModified: "10/02/2012", ProviderName: "Test1", Bid: "1.2", Bid1: "1.19", Bid2: "1.2", Ask: "2CloseBid: "32", CloseAsk: "35" },
              { CurrencyCode: "PHP", LastModified: "10/02/2012", ProviderName: "Test2", Other fields... }]

我想在javascript中进行此转换。 尝试使用stringify和parse但它没有提供所需的输出。 请建议

1 个答案:

答案 0 :(得分:0)

您可以使用for-in循环播放迭代每个key

let input = {
    "EUR": {
        "Description": "",
        "Bid": "1.1222",
        "Region": "",
        "Bid1": "1.1283",
        "CurrencyCode": "EUR",
        "FeedSource": "1",
        "Ask": "1.1226",
        "ProviderName": "TEST 1",
        "Low": "1.1195",
        "LastModified": "2014-05-20T08:11:13",
        "CreatedBy": "10000",
        "OpenBid": "1.12",
        "QuotedCurrencyCode": "USD"
    },
    "PHP": {
        "Description": "",
        "Bid": "46.75",
        "Region": "",
        "Bid1": "4.59",
        "CurrencyCode": "PHP",
        "FeedSource": "1",
        "Ask": "46.755",
        "ProviderName": "Test2",
        "Low": "4.715",
        "LastModified": "2016-05-20T07:54:32",
        "CreatedBy": "10000",
        "QuotedCurrencyCode": "USD"
    }
}

var output = [];
for(let key in input){
  output.push(input[key])
}
console.log(output);