如何将JavaScript中的JSON字符串转换为JSON对象数组?
web前端开发
共 1225字,需浏览 3分钟
·
2020-12-23 11:19
将JSON字符串转换为JSON对象数组
公众号:web前端开发
Click Here
<html>
<head>
<title>
将JSON字符串转换为JSON对象数组
title>
head>
<body style = "text-align:center;">
<h1 style = "color:#1ab1cc;" >
公众号:web前端开发
h1>
<p id = "GFG_UP">p>
<button onclick = "myGFG()">
Click Here
button>
<p id = "GFG_DOWN">p>
<script>
var up = document.getElementById("GFG_UP");
var JS_Obj =
'{"prop_1":"val_1", "prop_2":"val_2", "prop_3" : "val_3"}';
up.innerHTML = "JSON string - '" + JS_Obj + "'";
var down = document.getElementById("GFG_DOWN");
function myGFG() {
var obj = eval('(' + JS_Obj + ')');
var res = [];
for(var i in obj)
res.push(obj[i]);
down.innerHTML = "Array of values - ["
+ res + "]";
}
script>
body>
html>
评论