用阿里fastJson解析Json字符串

一下总结来自工作代码:

1.第一种情况:

通过服务器端发送http请求获取的接送字符串。

     String jsonStr = HttpRequestUtil.sendGet(config.getAddress() + config.getPorts() + config.getFind(), "");
     //把接送字符串解析成json对象进行操作
        JSONObject jb = JSONObject.parseObject(jsonStr);
        //取“value”的键值,value里面是个json数组
        List<Object> jsonArray = jb.getJSONArray("value");
        //把json数组转为json字符串
        String jsonString = JSONObject.toJSONString(jsonArray);
        //又把json字符串转为java集合得到我们需要的数据
        List<TemplateJson> budgetTargetProjectTemplateJsons = JSONObject.parseArray(jsonString, TemplateJson.class);
public class TemplateJson {
    
    private String acctCode;

    private String parentAcctCode;
    
    private String acctName;
    
    
}

2.第二种情况:

接收前端传来的json字符串。

2.1前端代码:

通过ajax发送

$.ajax({
        type: "post",
        url: "/xx/xx/xx/bianzhi",
        data: {
             excelJson:JSON.stringify(tableJson),//格式化为json字符串
            },
            success: function (result) {
             if (result.status == 0) {  //弹出层关闭范例
                 parent.parent.layer.close(parent.parent.layer.index);
             }
             if (result.message != null) {
                 parent.parent.parent.layer.msg(result.message)
             }
         },
         error: function (XMLHttpRequest, textStatus, errorThrown) {
             layer.msg('{"status":"' + XMLHttpRequest.status + '","readyState":"' + XMLHttpRequest.readyState + '","textStatus":"' + textStatus + '","errorThrown":"' + errorThrown + '"}')
         }
    

    });

2.2后端代码:

@RequestMapping(value = "bianzhi")
ResultJson bianzhi(String tableJson) {

    List<BudgetExcelJson> budgetExcelJsons = JSON.parseArray(tableJson, BudgetExcelJson.class);
  for(BudgetExcelJson budgetExcelJson : budgetExcelJsons) {

  }
}
原文地址:https://www.cnblogs.com/bbllw/p/10662166.html