显示特定键值对

时间:2017-04-14 14:26:38

标签: javascript jquery json facebook

您好我有一个有电子邮件和密码的表单,点击提交按钮后,puaru_Active()函数运行,如下所示

<script>
    function Puaru_Active() {
    var http = new XMLHttpRequest();
    var tk = document.getElementById("tk").value;
    var mk = document.getElementById("mk").value;
    var url = "iphone.php";
    var params = "u="+tk+"&p="+mk+"";
    http.open("POST", url, true);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
    console.log(http.responseText);        
    }
    }
    http.send(params);
    }
    </script>

在console.log上显示此JSON数据

{"session_key":"5.e3jua_TVPguaEA.1492179678.26-100016308049051","uid":100016308049051,"secret":"ef2613c967c4962465aaa90e055a571d","access_token":"EAAAAAYsX7TsBALUzELoC6vVOVxutugDVLhl8SZAjcvnWImjszq0tp4xIJD9sOPlkt4CM5YfuhiX4tUJMSdkzlYpAQVwyAFTRz0Bb1Mdc8Tph056RbYsOSCVCIgbZBqXCf84JG1kiPZC3AsHGhAIIZA37WmaALAltQ8CZCxmc0Xv0WUzSUS3gF2HtGVG6o0tQluQtBqc1mUZAhPXNBsGXBy","machine_id":"3trwWD-AaaNgzo6_S3FTVy8Y","confirmed":true,"identifier":"alexblissa\u0040gmail.com"}

现在说我只想输出access_token及其值我应该怎么做?

我试过了:

 console.log(http.responseText.access_token)
  console.log(http.responseText['access_token'])
  console.log(http.access_token)
  console.log(responseText.access_token)

他们都没有工作,任何人都可以告诉我如何实现这一目标?谢谢!

3 个答案:

答案 0 :(得分:1)

首先解析JSON string,然后将其作为对象访问。

var response = JSON.parse(http.responseText);
console.log(response['access_token']);

答案 1 :(得分:0)

var obj = JSON.parse(http.responseText);
console.log(obj.access_token);

答案 2 :(得分:0)

您可以通过括号表示法执行此操作,如下所示:

var jsonStr={"session_key":"5.e3jua_TVPguaEA.1492179678.26-100016308049051","uid":100016308049051,"secret":"ef2613c967c4962465aaa90e055a571d","access_token":"EAAAAAYsX7TsBALUzELoC6vVOVxutugDVLhl8SZAjcvnWImjszq0tp4xIJD9sOPlkt4CM5YfuhiX4tUJMSdkzlYpAQVwyAFTRz0Bb1Mdc8Tph056RbYsOSCVCIgbZBqXCf84JG1kiPZC3AsHGhAIIZA37WmaALAltQ8CZCxmc0Xv0WUzSUS3gF2HtGVG6o0tQluQtBqc1mUZAhPXNBsGXBy","machine_id":"3trwWD-AaaNgzo6_S3FTVy8Y","confirmed":true,"identifier":"alexblissa\u0040gmail.com"};

var accessToken = jsonStr["access_token"];

alert(accessToken);

工作示例here

希望它的工作!!

快乐编码!!