首页 >web前端 >js教程 >JSON取值前判断_json

JSON取值前判断_json

WBOY
WBOY原创
2016-05-16 16:24:551247浏览

复制代码代码如下:

public static void main(String[] args)抛出异常{
     字符串 jsonStr1="{"access_token":"OezXcEiiBSKSxW0eoylIeIVb08lFhyeFsq6cUJJNo8XYLoJjqYARJOZUrnqhfvnyMKOSjz5SHPHGUmt6f09_762MWQ0HLnZtffDPyD2A6TgCqWlZAEsHLhIaaQVYx1lywxOWQ Trr7rX13i_w1INGnw","expires_in":7200,"openid":"owYTUs68SXYX83wBvm_uN4Ya****","scope":"snsapi_base"}";   
    字符串 jsonStr2="{"access_token":"OezXcEiiBSKSxW0eoylIeIVb08lFhyeFsq6cUJJNo8XYLoJjqYARJOZUrnqhfvnyMKOSjz5SHPHGUmt6f09_762MWQ0HLnZtffDPyD2A6TgCqWlZAEsHLhIaaQVYx1lywxOW QTrr7rX13i_w1INGnw","expires_in":7200,"refresh_token":"OezXcEiiBSKSxW0eoylIeIVb0 8lFhyeFsq6cUJJNo8XYLoJjqYARJOZUrnqhfvnyuq9ojibRxolf9ElS7PSM0Kz1OTNlJ8ZKJl2BfbPi0 LA-nQ9uNVUE8ujLFsdCeP79_L9FmIYcmHLXWXnl-fUsVg","openid":"owYTUs68SXYX83wBvm_uN4Y****","scope":"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-nQ9uNVUE8ujLFsdCeP79_L9FmIYcmHLXWXnl-fUS VG

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn