JSON取值前推断

日期:2019-09-07编辑作者:关于计算机

复制代码 代码如下:

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);
}

运行结果:

复制代码 代码如下:

null
OezXcEiiBSKSxW0eoylIeIVb08lFhyeFsq6cUJJNo8XYLoJjqYARJOZUrnqhfvnyuq9ojibRxolf9ElS7PSM0Kz1OTNlJ8ZKJl2BfbPi0LA-nQ9uNVUE8ujLFsdCeP79_L9FmIYcmHLXWXnl-fUsVg

复制代码 代码如下: public static void main(String[] args)throws Exception{ String jsonStr1="{"access_token":"OezXcEiiBSKSxW0eoylIe...

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

您可能感兴趣的文章:

  • java json字符串转JSONObject和JSONArray以及取值的实例
  • js遍历json对象所有key及根据动态key获取值的方法(必看)
  • 浅谈json取值(对象和数组)
  • jquery实现下拉菜单的二级联动利用json对象从DB取值显示联动
  • 当json键为数字时的取值方法解析
  • 百度编辑器从Json对象中取值,完成初次渲染,在编辑器内画表格
  • 实例详解JSON取值(key是中文或者数字)方式

本文由今晚最快开奖现场直播发布于关于计算机,转载请注明出处:JSON取值前推断

关键词:

使用javascript获取页面名称

代码很简单,就不多废话了,奉上代码: 复制代码 代码如下: // 取当前页面名称(不带后缀名) function getPageName1() {  ...

详细>>

格局用法实例

本文实例讲述了jQuery中height()方法用法。分享给大家供大家参考。具体分析如下: 本文实例讲述了jQuery中outerHeight()方...

详细>>

javascript实现分享,javascript实现微信分享

代码很简单,笔者这里就非常少废话了,直接奉上源码: javascript落实微信分享,javascript达成分享 代码很简短,作者...

详细>>

方法用法实例

本文实例讲述了jQuery中hide()方法用法。分享给大家供大家参考。具体分析如下: jQuery中hide()方法用法实例,jqueryhid...

详细>>