首頁  >  文章  >  web前端  >  JSON取值前判斷_json

JSON取值前判斷_json

WBOY
WBOY原創
2016-05-16 16:24:551231瀏覽

複製程式碼程式碼如下:

public static void main(String[] args)傳送異常{
     字串 jsonStr1="{"access_token":"OezXcEiiBSKSxW0eoylIeIVb08lFhyeFsq6cUJJNo8XYLoJjqYARJOZUrnqhfnyMKOSjz5SHPHGUmt6f09_762ffMWQ0HLnHLKx rX13i_w1INGnw","expires_in":7200,"openid":"owYTUs68SXYX83wBvm_uN4Ya****","scope":"snsapi_base"}";   
    字串jsonStr2="{"access_token":"OezXcEiiBSKSxW0eoylIeIVb08lFhyeFsq6cUJJNo8XYLoJjqYARJOZUrnqhfvnyMKOSjz5SHPHGUmt6f09_762MWQ021Kx 7rX13i_w1INGnw","expires_in":7200,"refresh_token":"OezXcEiiBSKSxW0eoylIeIVb0 8lFhyeFsq6cUJJNo8XYLoJjqYARJOZUrnqhfvn uNVUE8ujLFsdCeP79_L9FmIYcmHLXWXnl-fUsVg","openid":"owYTUs68SXYX83wBvm_uN4Y****","範圍" :"snsapi_base" }";   
    JSONObject json1=JSONObject.fromObject(jsonStr1);
    JSONObject json2=JSONObject.fromObject(jsonStr2);
    字串refresh_token_1=null;
    字串refresh_token_2=null;
    if(json1.containsKey("refresh_token")){
        fresh_token_1=json1.getString("refresh_token");
    }
    if(json2.containsKey("refresh_token")){
        fresh_token_2=json2.getString("refresh_token");
    }
   
    System.out.println(refresh_token_1);
    System.out.println(refresh_token_2);
}

運行結果:

複製程式碼程式碼如下:


OezXcEiiBSKSxW0eoylIeIVb08lFhyeFsq6cUJJNo8XYLoJjqYARJOZUrnqhfvnyuq9ojibRxolf9ElS7PSM0Kz1OTNlJ8ZKJl2BfbPi0LA-UEnQA4

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn