如何在JavaScript或jQuery中将JSON数组转换为JSON对象?

时间:2016-10-14 07:59:06

标签: javascript jquery html json

如何将JSON数组转换为JSON对象。例如,我创建了一个包含JSON数组的变量:

[{  "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check"}]

如何将实体转换为JSON对象,将实体作为JSON对象,如下所示:

{"Entities ":[{  "Bank Account Name": "State Bank",
        "Currency Code": "4000",
        "Deposit Date": "5/2/1794",
        "Payment Channel": "check"}]
}

有办法做到这一点吗?我尝试了Stringifyparse

4 个答案:

答案 0 :(得分:2)



var array = [{  "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check"}];

var obj = {"Entities" : array};

console.log(obj);




答案 1 :(得分:2)

var original  = [{  "Bank Account Name": "State Bank",
"Currency Code": "4000",
"Deposit Date": "5/2/1794",
"Payment Channel": "check"}];

var newValue = JSON.stringify({Entities:[original[0]]});

console.log(newValue);
//{"Entities":[{"Bank Account Name":"State Bank","Currency Code":"4000","Deposit Date":"5/2/1794","Payment Channel":"check"}]}

答案 2 :(得分:0)

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
  var obj = [{  "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check"}];
var objNew={};
objNew.Entitys=obj;

alert(JSON.stringify(objNew))
});
</script>
</head>
<body>

</body>
</html>

答案 3 :(得分:0)

简单地包装它:

var array = [{  "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check"}];

var jsonObject = {"Entities":array};