如何在JavaScript中创建和空的JSON并动态添加键和其他JSON对象?
如何在JavaScript中创建和空的JSON并动态添加键和其他JSON对象?
mydata = {}
我
newdata = {
'first': [ {'1': 'test'} ]
}
尝试了这个
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
const myJSON = '{}';
myJSON['first']['1'] = 'test';
const myObj = JSON.parse(myJSON);
let text = "";
for (const x in myObj) {
text += x + ", ";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
How to create and empty JSON in JavaScript and add keys and additional JSON objects dynamically?
mydata = {}
to
newdata = {
'first': [ {'1': 'test'} ]
}
I tried this
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
const myJSON = '{}';
myJSON['first']['1'] = 'test';
const myObj = JSON.parse(myJSON);
let text = "";
for (const x in myObj) {
text += x + ", ";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将json解析到一个物体中,然后设置他的属性
Parse the json into an object, then set his properties
尝试一下
try this