获取的string转JSONArray或JSONObject

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

² 返回值: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": []

}

]

                 }

           ]

}]

}





下面获得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来承接。


猜你喜欢

转载自blog.csdn.net/qq_23145857/article/details/78189551