使用Java(完整代码)
项目结构:
java源码:
//导入的库仅供参考
import com.sun.corba.se.impl.orbutil.ObjectStreamClassUtil_1_3;
import jdk.nashorn.internal.parser.JSONParser;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Iterator;
/**
* readJSON
* <h5>描述:</h5>
*/
public class readJSON
{
public static void main(String[] args)
{
String jsonStr = readJsonFile("D:\\software\\computer_software\\java\\code\\jsonDemo01\\src\\maintain_order.json");
try
{
JSONObject obj = new JSONObject(jsonStr);
String str = obj.getString("aaData");
JSONArray aaObj = new JSONArray(str);
// System.out.println(aaObj.length());
int cntAll = 0;
for (int i = 0, len1 = aaObj.length(); i < len1; ++i) {
++cntAll;
int cntCur = 0;
for (Iterator<String> it = aaObj.getJSONObject(i).keys(); it.hasNext(); )
{
++cntCur;
String key = it.next();
String val = aaObj.getJSONObject(i).getString(key);
System.out.println(key + " = " + val);
}
System.out.println("================== Total: " + cntCur + " ==================");
}
System.out.println("================== aaData Total: " + cntAll + " ==================");
}
catch (JSONException e)
{
e.printStackTrace();
}
}
public static String readJsonFile(String filePath)
{
String jsonStr = "";
try
{
File jsonFile = new File(filePath);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1)
{
sb.append((char) ch);
}
reader.close();
jsonStr = sb.toString();
return jsonStr;
}
catch (Exception ex)
{
ex.printStackTrace();
return null;
}
}
}
注意:.json文件使用相对路径时应该按照设置的out目录下的来做(没记错的话)
使用JS
<%@page contentType="text/html; charset=UTF-8" %>
<meta charset="UTF-8"/>
<button type="button" id="get_data_button" name="get_data_button" onclick="javascript:getData();">从后端获取json</button>
<button type="button" id="send_data_button" name="send_data_button" onclick="javascript:sendData();">发送json到后端</button>
<div id="message" name="message"></div>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script>
var jsonObj = undefined;
function getData() {
$.post("maintain_order.json", function (json) {
// console.log("前端收到:" + JSON.stringify(json));
jsonObj = JSON.parse(JSON.stringify(json));
var cntAll = 0;
for (var i in jsonObj.aaData) {
++cntAll;
var cntCur = 0
for (var j in jsonObj.aaData[i]) {
++cntCur;
console.log(j + " = " + jsonObj.aaData[i][j]);
}
console.log("=====================Total: " + cntCur + "================");
}
console.log("==============" + "Sum of aaData: " + cntAll + "==============");
// console.log(jsonObj);
// console.log(json)
$("#message").html("前端收到:" + JSON.stringify(json));
});
}
function sendData() {
//var data=jsonObj;
var data = {"json_data_str": JSON.stringify(jsonObj)};
$.post("jq_back.jsp", data, function (json) {
console.log("后端返回:" + JSON.stringify(json));
$("#message").html("后端返回:" + JSON.stringify(json));
});
}
</script>