解析類似'a=b&c=d&e=f'格式,幷包含json數據的格式

例子

access_token = 1869091015537008930192538665838630977636416301853457954822505576 

app_key = 650 ho335a0y9n2f1ts5g5wg28me11175 
&
app_secret_key= NDA2MTE1NDQ3MzgzMzAwMDg2NzAyNDE0NzMzOTAzMzAyNjk5NzU1NzY5MDE2MTEzMjA2NzM5Nzk3MTQzMzAxNw == 

biz_content = {
    "notify_code": "04",
    "custom_content": {
        "posList": [{
            "posCode": "1",
            "posVer": "-"
        }, {
            "posCode": "2",
            "posVer": "-"
        }, {
            "posCode": "3",
            "posVer": "-"
        }, {
            "posCode": "999",
            "posVer": "-"
        }]
    }
} & method = synjones.cloudcard.pollgw.push & msg_id = a0b5f0f6 - 9 ca7 - 4 c77 - bd9d - 5 ffac4dc6c63

1、先將數據轉爲map(以上整體數據爲'msg'代替)

      Map map = new HashMap();
      String[] allStrings = msg.split("&");
        if (allStrings != null && allStrings.length != 0) {
            for (String strings : allStrings) {
                if (strings != null && strings.trim().length() != 0) {
                    String[] x = strings.split("=");
                    if (x != null && x.length == 2) {
                        map.put(x[0], strings.substring(strings.indexOf("=") + 1));
                    }
                }
            }
        }

 此時已經將整體數據封裝爲map

2、通過key獲取value(biz_content)

      Object object=map.get("biz_content");

3、利用fastjson獲取json中的數據

               //com.alibaba.fastjson.JSONObject                 


                JSONObject parseObject = JSONObject.parseObject(object.toString());
                Object object3 = parseObject.get("custom_content");
                
                JSONObject parseObject1 = JSONObject.parseObject(object3.toString());
                Object object31 = parseObject1.get("posList");

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章