首页 > 代码库 > JSON取值前判断
JSON取值前判断
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);}
运行结果:nullOezXcEiiBSKSxW0eoylIeIVb08lFhyeFsq6cUJJNo8XYLoJjqYARJOZUrnqhfvnyuq9ojibRxolf9ElS7PSM0Kz1OTNlJ8ZKJl2BfbPi0LA-nQ9uNVUE8ujLFsdCeP79_L9FmIYcmHLXWXnl-fUsVg
JSON取值前判断
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。