开发者

jQuery json.text?

开发者 https://www.devze.com 2023-04-13 07:42 出处:网络
I\'m trying to do a connect page where you can view everything for twitter, facebook, youtube, ... I\'m using the getJson function in jquery to get the data from the twitterApi:http://api.twitter.com/

I'm trying to do a connect page where you can view everything for twitter, facebook, youtube, ... I'm using the getJson function in jquery to get the data from the twitterApi:http://api.twitter.com/1/users/show.json?screen_name=Someusername. The issue that I'm having is that I can get all the data in the returned json file except for the text string, which contains the latest post. When I try to get json.text, it returns undefined, which is not true because I have visited the returned json straight through my browser before. I think that the default jQuery.text() function is messing with it, but I don't have any proof. If that's how it is, does anyone know a work around?

Code (twitter.php is just a php file that copies the data from the twitter site to avoid cross domain scripting issues):

function setUser() {
    $('#intro').fadeOut();
    $('body').css('background-color', "#" + bg);
    $('body').css('background-image', 'url(' + bgImg + ')');
    $('body').css('background-repeat', 'repeat');
    $('.imgwrap').html('<img class="profimg" src="' + ppic + '"/>');
    $('.username').text(username);
}

var bg = "white";
var ppic = "http://test.campatet.com/identicon/ident.php?size=48";
var bgImg = "none";
var username = "undefined";
$(function () {
    $('#submitid').click(function () {
        $('#login, #submitid, #introwrapper label').hide();
        $('#spinner').spin();
        $('#intro').css("width", "99%").width("100%");
        $.getJSON('twitter.php?f=show&p="1/users/"&q="screen_name=' + $('#login').val() + '"', function (json) {
            bg = json.profile_background_color;
            alert(json.text);

            if (json.profile_image_url) {
                ppic = json.profile_image_url;
            }
            if (json.profile_background_image_url) {
                bgImg = json.profile_background_image_url;
            }

            if (json.error) {
                username = json.error;
                $('#introwrapper').html('Error: ' + username + '. <a href="javascript:location.reload(true);" style="color:white;">Refresh Page</a>');
            } else {
                username = json.name;
                setUser();
            }
     开发者_运维技巧   });
    });
});


After inspecting the response JSON string, I've concluded that you have to use json.status.text instead of json.text.

See below for the structure of the response JSON:

{
    "default_profile_image": true,
    ...
    "profile_image_url_https": "https:\/\/si0.twimg.com\/sticky\/default_profile_images\/default_profile_4_normal.png",
    "status": {
        ...
        "text": "woah i forgot i made a twitter account... i promised myself i'd never do this.. oh well i give in haha."
    },
    ...
}

Fiddle: http://jsfiddle.net/jmwy4/
It's also possible to use http://jsbeautifier.org/ for indenting a JSON response, to get a clear view of the returned JSON object/response.

0

精彩评论

暂无评论...
验证码 换一张
取 消