利用Java/JS解析二维JSON

使用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>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值