码迷,mamicode.com
首页 > Web开发 > 详细

JSON取值前判断

时间:2014-11-26 13:59:04      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   on   div   

public static void main(String[] args)throws Exception{
     String jsonStr1="{\"access_token\":\"OezXcEiiBSKSxW0eoylIeIVb08lFhyeFsq6cUJJNo8XYLoJjqYARJOZUrnqhfvnyMKOSjz5SHPHGUmt6f09_762MWQ0HLnZtffDPyD2A6TgCqWlZAEsHLhIaaQVYx1lywxOWQTrr7rX13i_w1INGnw\",\"expires_in\":7200,\"openid\":\"owYTUs68SXYX83wBvm_uN4Ya****\",\"scope\":\"snsapi_base\"}";    
    String jsonStr2="{\"access_token\":\"OezXcEiiBSKSxW0eoylIeIVb08lFhyeFsq6cUJJNo8XYLoJjqYARJOZUrnqhfvnyMKOSjz5SHPHGUmt6f09_762MWQ0HLnZtffDPyD2A6TgCqWlZAEsHLhIaaQVYx1lywxOWQTrr7rX13i_w1INGnw\",\"expires_in\":7200,\"refresh_token\":\"OezXcEiiBSKSxW0eoylIeIVb08lFhyeFsq6cUJJNo8XYLoJjqYARJOZUrnqhfvnyuq9ojibRxolf9ElS7PSM0Kz1OTNlJ8ZKJl2BfbPi0LA-nQ9uNVUE8ujLFsdCeP79_L9FmIYcmHLXWXnl-fUsVg\",\"openid\":\"owYTUs68SXYX83wBvm_uN4Y****\",\"scope\":\"snsapi_base\"}";    
    JSONObject json1=JSONObject.fromObject(jsonStr1);
    JSONObject json2=JSONObject.fromObject(jsonStr2);
    String refresh_token_1=null;
    String refresh_token_2=null;
    if(json1.containsKey("refresh_token")){
        refresh_token_1=json1.getString("refresh_token");
    }
    if(json2.containsKey("refresh_token")){
        refresh_token_2=json2.getString("refresh_token");
    }
    
    System.out.println(refresh_token_1);
    System.out.println(refresh_token_2);
}
运行结果:
null
OezXcEiiBSKSxW0eoylIeIVb08lFhyeFsq6cUJJNo8XYLoJjqYARJOZUrnqhfvnyuq9ojibRxolf9ElS7PSM0Kz1OTNlJ8ZKJl2BfbPi0LA-nQ9uNVUE8ujLFsdCeP79_L9FmIYcmHLXWXnl-fUsVg

JSON取值前判断

标签:style   blog   io   ar   color   os   sp   on   div   

原文地址:http://www.cnblogs.com/qq-757617012/p/4122790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!