编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

fastJson解析多层Json数据获取关键字段信息

wxchong 2025-03-05 22:11:42 开源技术 28 ℃ 0 评论
  • 需求:根据物料信息获取子配件物料类型,接口是用友U8接口

根据物料信息获取子配件信息,下面就是需要解析的json数据:

{
    "returnCode":0,
    "returnMessage":"成功",
    "fileInfo":{
        "cInvCode":"G21501161",
        "cInvAddCode":"BRG-1161",
        "cInvName":"IMN-S6M-SPA-800玻璃组件",
        "cInvStd":null,
        "cInvUnit":"PCS",
        "iBomType":1,
        "iVersionNO":10,
        "cVersionDesc":"A0",
        "dVersionEffDate":"2000-01-01",
        "dVersionEndDate":"2099-12-31",
        "cIdentCode":null,
        "cIdentDesc":null,
        "cCreateUser":"801126",
        "dCreateTime":"2019-07-08 17:23:46",
        "cModifyUser":null,
        "dModifyTime":null,
        "cRelsUser":"803466",
        "dRelsTime":"2019-08-30 15:13:02",
        "cCloseUser":null,
        "dCloseTime":null,
        "iStatus":3,
        "fileList":[
            {
                "iSortSeq":10,
                "cOpSeq":"0000",
                "cInvCode":"G30200101219",
                "cInvAddCode":"LB-4013",
                "cInvName":"6mm平玻",
                "cInvStd":"1949*708*6",
                "cInvUnit":"平方米",
                "cInvAssUnit":"PCS",
                "iChangeRate":1.38,
                "iFVFlag":1,
                "iBaseQtyN":1.38,
                "iBaseQtyD":1,
                "iCompScrap":0,
                "iAssBaseQtyN":1,
                "iWIPType":3,
                "cDepCode":null,
                "cDepName":null,
                "cWhCode":null,
                "cWhName":null,
                "cRemark":null,
                "cInvDefine1":"光玻",
                "cInvDefine2":null,
                "cInvDefine3":null,
                "cInvDefine4":null,
                "cInvDefine5":null,
                "cInvDefine6":null,
                "cInvDefine7":null,
                "cInvDefine8":null,
                "cInvDefine9":null,
                "cInvDefine10":null,
                "cInvDefine11":null,
                "cInvDefine12":null,
                "cInvDefine13":null,
                "cInvDefine14":null,
                "cInvDefine15":null,
                "cInvDefine16":null,
                "cDefine22":"RS-EF单面",
                "cDefine23":null,
                "cDefine24":"1.000000",
                "cDefine25":"0.000",
                "cDefine26":1,
                "cDefine27":null,
                "cDefine28":"PCS",
                "cDefine29":null,
                "cDefine30":null,
                "cDefine31":null,
                "cDefine32":null,
                "cDefine33":null,
                "cDefine34":null,
                "cDefine35":null,
                "cDefine36":null,
                "cDefine37":null
            }
        ]
    }
}

目的是要获取"cInvCode":"G30200101219"这个信息的前7位;

  • 首先理解JSONArrary和JsonObject

1、JSONObject和JSONArray的数据表示形式

JSONObject的数据是用 { } 来表示的,

例如: { "id":1,"name":"李四" }

而JSONArray,顾名思义是由JSONObject构成的数组,用 [ { } , { } , ...... , { } ] 来表示

例如: [ { "id":1,"name":"李四" } , { "id":2,"name":"王二" } } ] ;

表示了包含2个JSONObject的JSONArray。

可以看到明显的区别,里面用的是多个 { } ,最外面用的是 [ ] ;

2、如何从字符串String获得JSONObject对象和JSONArray对象

JSONObject jsonObject = new JSONObject ( String str);

JSONArray jsonArray = new JSONArray(String str ) ;

3、如何从JSONArray中获得JSONObject对象

大家可以把JSONArray当成一般的数组来对待,只是获取的数据内数据的方法不一样

JSONObject jsonObject = jsonArray.getJSONObject(i) ;

4、获取JSON内的数据

int id= jsonObject.getInt ( "id" ) ; // 这里的id得到的数据就是1.

String name = jsonObject.getString( " name") ; // 这里的name得到的数据就是李四


  • 解析数据
public String getGlassType(String sku){
		String json = HttpUtils.sendPost(u8murl+"getU8InventoryBOM", "{\"userCode\":\"admin\",\"terminalCaption\":\"001122334455\",\"operationCode\":101,\"fileCode\":\""+sku+"\"}");
		JSONObject jb = JSON.parseObject(json);
		JSONArray glassTypeArr = jb.getJSONObject("fileInfo").getJSONArray("fileList");
		for (int i = 0; i < glasstypearr.size i string getglasstypetype='glassTypeArr.getJSONObject(i).getString("cInvCode");' ifgetglasstypetype.substring0 2.equalsg3 return getglasstypetype.length>7?getGlassTypetype.substring(0, 7):"";
		}
		return "";
	}

返回结果



  • 戏说fastjson

漏洞之王漏穿肠:fastjson,罪魁祸首就是autoType特性。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表