javascript中把数据封装成json数据格式
直接po代码和截图
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>这里填写标题</title>
<meta name="keywords" content="这里填写关键词" />
<meta name="description" content="这里填写说明内容" />
<script language="JavaScript" type="text/javascript">
function saveAsObject(){
var obj1 = document.getElementById("input1");
var data = [];
var person1 = new Object();
person1.name=obj1.value;
person1.age=18;
person1.sex="男";
data.push(person1);
var person2 = new Object();
person2.name="小丽";
person2.age=20;
person2.sex="女";
data.push(person2);
var json_str = JSON.stringify(data);
alert("json_str = " + json_str);
}
</script>
<style type="text/css">
<!--CSS样式代码位置-->
</style>
</head>
<body>
<input type="text" id="input1"/>
<input type="button" value="button-me" onclick="saveAsObject()" />
</body>
</html>
下面是截图