前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >获取的string转JSONArray或JSONObject

获取的string转JSONArray或JSONObject

作者头像
全栈程序员站长
发布2022-08-31 13:50:29
7620
发布2022-08-31 13:50:29
举报

大家好,又见面了,我是你们的朋友全栈君。

停车场收费标准典型案例:

² 返回值:JSON格式字符串

{

“serviceId”:”3c.park.queryparkstandard“,

“resultCode”:0,

“message”:”成功“,

“dataItems”:[{

“objectId”:””,

“operateType”:”READ”,

“attributes”:{

“parkCode”: “park01”,

“parkName”: “KKMALL_PARK”,

“tempStandard“: “临时卡收费标准说明“

},

“subItems”: [

{

“objectId”: “”,

“operateType”: “READ”,

“attributes”: {

“cardType”: “月卡A“

},

“subItems”: [

{

“objectId”: “”,

“operateType”: “READ”,

“attributes”: {

“monthPeriod“:1,

“money“:150

},

“subItems”: []

}

]

},

{

“objectId”: “”,

“operateType”: “READ”,

“attributes”: {

“cardType”: “月卡B“

},

“subItems”: [

{

“objectId”: “”,

“operateType”: “READ”,

“attributes”: {

“monthPeriod“:1,

“money“:100

},

“subItems”: []

}

]

}

]

}]

}

获取的string转JSONArray或JSONObject
获取的string转JSONArray或JSONObject
获取的string转JSONArray或JSONObject
获取的string转JSONArray或JSONObject

下面获得dataItems的内容,及转换

String jsonString= queryParkStandardServices.execute(params,type);

JSONObject jsonobject=new JSONObject(); jsonobject.put(“json0”, jsonString);//如果String是[{},..]这种形式的,可以形成一个新的单json {json0:…} System.out.println(“_________0824———-“+jsonString); JSONArray jsonarray=new JSONArray(); jsonarray.add(jsonobject);//如果jsonobject里的value是{json0:[{},..]}这种形式的就用JSONArray来承接 JSONObject obResult = (JSONObject) jsonarray.get(0); JSONArray jsonarray01=obResult.getJSONArray(“json0”); JSONObject json01 = (JSONObject) jsonarray01.get(0); JSONObject partone=json01.getJSONObject(“attributes”); String parkCodes =(String) partone.get(“parkCode”);//停车场编号 String parkName=(String) partone.get(“parkName”);//停车场名字 String tempStandard=(String) partone.get(“tempStandard”);//临时卡收费说明 JSONArray parttwo=json01.getJSONArray(“subItems”); List<FeeStandard> FeeStandards=new ArrayList<>(); for(int i=0;i<parttwo.size();i++){ FeeStandard feeStandard=new FeeStandard(); feeStandard.setParkCode(parkCodes); feeStandard.setParkName(parkName); feeStandard.setTempStandard(tempStandard); JSONObject obResult2 = new JSONObject(); obResult2= (JSONObject) parttwo.get(i); JSONObject attributes=(JSONObject) obResult2.get(“attributes”); String cardType0=(String) attributes.get(“cardType”);//月卡类型 JSONArray subItems= (JSONArray) obResult2.getJSONArray(“subItems”); JSONObject subItems0 = (JSONObject) subItems.get(0); JSONObject attributes1=(JSONObject) subItems0.get(“attributes”); int monthPeriod0=(int) attributes1.get(“monthPeriod”);//月卡收费周期 Double money0=(Double) attributes1.get(“money”);//月卡收费标准 feeStandard.setCardType(cardType0); feeStandard.setMoney(money0); feeStandard.setMonthPeriod(monthPeriod0); FeeStandards.add(feeStandard);

}

注:JSONObject 和JsonObject 都是json对象,但获取对象里面元素是需要用不同对象来承接。

String results = EntityUtils.toString(response.getEntity()); JsonObject json=new JsonParser().parse(results).getAsJsonObject(); int resultCode=json.get(“resultCode”).getAsInt(); if(resultCode==0){ JsonElement dataItems=json.get(“dataItems”);

}

JsonObject get到的元素用JsonElement 来承接。

JSONObject partone=json01.getJSONObject(“attributes”); String orderNo=(String) partone.get(“orderNo”);//订单编号 String carNo1=(String) partone.get(“carNo”);//车牌号 String totalFee=(String) partone.get(“totalFee”);//总费用

JSONObject get到的元素用String来承接。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/151417.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档