Facebook API Javascript JSON响应 [英] Facebook API Javascript JSON response

查看:138
本文介绍了Facebook API Javascript JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function getUser()
{
    FB.api('/me', function(response) {
       console.log('Response is '+response);
       alert('Your name is ' + response.first_name);
       alert('Your last name is ' + response.last_name);
       alert('Your Gender is ' + response.gender);
       alert('Your status is '+response.username);
    }

如何获得以下打印的整个回复?

How can I get the entire response like this below printed?

{
  "id": "blah blah", 
  "name": "blah blah", 
  "first_name": "blah blah", 
  "last_name": "blah blah", 
  "link": "https://www.facebook.com/blah blah", 
  "username": "blah blah", 
  "hometown": {
    "id": "106442706060302", 
    "name": "Pune, Maharashtra"
  }, 
  "location": {
    "id": "106377336067638", 
    "name": "Bangalore, India"
  }, 
  "bio": "╔══╗♫ ♪♫\n║██║\n║¨o•♫\n╚═|̲̅̅●̲̅̅|̅lιlllι ♫ I LoVe MuZiK!!\n\n█║▌│█│║▌║│█║▌│║\n® Copyright © 2012 ™\n█║▌│█│║▌║│█║▌│║\n\nReVoLt", 
  "gender": "male", 
  "relationship_status": "Single", 
  "timezone": 5.5, 
  "locale": "en_GB", 
  "verified": true, 
  "updated_time": "2012-06-15T05:33:31+0000", 
  "type": "user"
}

还可以通过respose.name获取用户的名称。
如何获取参数位置中的位置,因为它是一个JSON数组?

Also by respose.name am getting the name of the user. How can I get the location in the parameter location as it is a JSON array?

推荐答案

使用JSON.stringify (响应);
它应该打印整个对象。

use JSON.stringify(response); it should print the entire object.

使用JSON.parse()或jQuery.parseJSON();解析并获取响应对象的任何属性
示例:jsfiddle.net/epinapala/HrfkL和这里:jsfiddle.net/epinapala/zfWWv/3

use JSON.parse() or jQuery.parseJSON(); to parse and get any properties of the response object examples here : jsfiddle.net/epinapala/HrfkL and here : jsfiddle.net/epinapala/zfWWv/3

    var obj2 = JSON.parse('{"id":"579156356","name":"Vishal Jethani","first_name":"Vishal","last_name":"Jethani","link":"https://www.facebook.com/vishal.jethani","username":"vishal.jethani","hometown":{"id":"106442706060302","name":"Pune, Maharashtra"},"location":{"id":"106377336067638","name":"Bangalore, India"},"bio":"bye bye to bad characters","gender":"male","relationship_status":"Single","timezone":5.5,"locale":"en_GB","verified":true,"updated_time":"2012-06-15T05:33:31+0000","type":"user"}');
alert("Parsing with Json : " + obj2.location.name);​

对于请求者要求的多维数组:
这应该工作: http://jsfiddle.net/epinapala / WQcDg /

For multidimensional array as asked by the requester : THis should work : http://jsfiddle.net/epinapala/WQcDg/

var obj2 = JSON.parse('{"work":[{"employer":{"id":"185998961446429","name":"Pvt Ltd"}}]}');

alert("Parsing with Json : " + JSON.stringify(obj2.work[0].employer.name));

这篇关于Facebook API Javascript JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆