Json数据格式的封装
//一个Json对象
// String resultJson="{\"name\":\"张三\",\"age\":22}";
JSONObject resultJson=new JSONObject();
resultJson.put("name", "张三");
resultJson.put("age", 22);
//Json数组
JSONObject resultJson=new JSONObject();
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject1=new JSONObject();
jsonObject1.put("name", "张三");
jsonObject1.put("age", 22);
JSONObject jsonObject2=new JSONObject();
jsonObject2.put("name", "李四");
jsonObject2.put("age", 23);
JSONObject jsonObject3=new JSONObject();
jsonObject3.put("name", "王五");
jsonObject3.put("age", 24);
jsonArray.add(jsonObject1);
jsonArray.add(jsonObject2);
jsonArray.add(jsonObject3);
//Json嵌套
JSONObject resultJson=new JSONObject();
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject1=new JSONObject();
jsonObject1.put("name", "张三");
jsonObject1.put("age", 22);
JSONObject scoreObject1=new JSONObject();
scoreObject1.put("chinese", 90);
scoreObject1.put("math", 100);
scoreObject1.put("english", 80);
jsonObject1.put("score", scoreObject1);
JSONObject jsonObject2=new JSONObject();
jsonObject2.put("name", "李四");
jsonObject2.put("age", 23);
JSONObject scoreObject2=new JSONObject();
scoreObject2.put("chinese", 70);
scoreObject2.put("math", 90);
scoreObject2.put("english", 90);
jsonObject2.put("score", scoreObject2);
JSONObject jsonObject3=new JSONObject();
jsonObject3.put("name", "王五");
jsonObject3.put("age", 24);
JSONObject scoreObject3=new JSONObject();
scoreObject3.put("chinese", 80);
scoreObject3.put("math", 60);
scoreObject3.put("english", 90);
jsonObject3.put("score", scoreObject3);
jsonArray.add(jsonObject1);
jsonArray.add(jsonObject2);
jsonArray.add(jsonObject3);
resultJson.put("students", jsonArray);
var strwhere = "recordType:\""+recordType+"\",recordId:\""+ids+"\",isRecord:\""+isRecord;
strwhere+="\"";
private JSONObject objWhere = null;
try {
objWhere = new JSONObject("{" + strWhere + "}");
} catch (JSONException e) {}
try {
String recordType= objWhere.getString("recordType");
String isRecord= objWhere.getString("isRecord");
}
} catch (JSONException e) {
e.printStackTrace();
}
前台
var dataObj = eval("("+data+")") Json串转成Json对象
<table id="studentTable" align="center" border="1px;" cellpadding="0px;">
<tr>
<th>姓名</th><th>年龄</th><th>得分</th>
</tr>
</table>
var dataObj=eval("("+xmlHttp.responseText+")");
var st=document.getElementById("studentTable");
alert(dataObj.students.length);
var newTr; // 行
var newTd0; // 第一列
var newTd1; // 第二列
var newTd2; // 第三列
for(var i=0;i<dataObj.students.length;i++){
var student=dataObj.students[i];
newTr=st.insertRow();
newTd0=newTr.insertCell();
newTd1=newTr.insertCell();
newTd2=newTr.insertCell();
newTd0.innerHTML=student.name;
newTd1.innerHTML=student.age;
newTd2.innerHTML="语文:"+student.score.chinese+",数学:"+student.score.math+",英语:"+student.score.english;
}
添加表单行
<script type="text/javascript">
function addOptions(){
//创建一个新的节点
//第一种方式
/*var option = document.createElement("option") ;
option.value = "小学" ;
option.text = "小学" ;
//拿到下拉框,加入小学
document.getElementsByTagName("select")[0].options.add(option) ;*/
//第二种方式
//获得select对象
//var select = document.getElementsByTagName("select")[0] ;
//select.innerHTML = select.innerHTML + "<option value = '小学'>小学</option>" ;
//第三种
var option = document.createElement("option") ;
option.value = "小学" ;
option.text = "小学" ;
//获得select对象
var select = document.getElementsByTagName("select")[0] ;
select.appendChild(option);
}
function fun(){
//拿到div对象
var d = document.getElementById("d") ;
//创建一个img标签对象
var img = document.createElement("img") ;
//指定属性
img.src = "images/1.jpg" ;
img.style.width = "200px" ;
img.height = "300" ;
//将图片添加到div标签中
d.appendChild(img) ;
}
</script>
<body>
<input type="button" value="添加选项" onclick="addOptions()"/>
<input type="button" value="添加一副图片" onclick="fun()">
<select>
<option>本科</option>
<option>专科</option>
<option>高中</option>
<option>初中</option>
</select>
<div id = "d"></div>
</body>