如何将url中的json对象作为字符串进行回显

时间:2016-11-11 19:17:58

标签: php json

我用它来从url获取json:

$json_string = file_get_contents('https://www.instagram.com/testuser/?__a=1');
$parsed_json = json_decode($json_string, true);

var_dump($parsed_json);

我得到了json回复:

    {"user": 
 {"username": "testuser", "has_blocked_viewer": false, "follows":
 {"count": 94}, "requested_by_viewer": false, "followed_by":
 {"count": 3}, "country_block": null, "has_requested_viewer": false,
               "external_url_linkshimmed": null, "follows_viewer": false,
               "profile_pic_url_hd": "https://scontent-lhr3-1.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg",
               "profile_pic_url": "https://scontent-lhr3-1.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg",
               "is_private": true, "full_name": null, "media":
 {"count": 0, "page_info":
 {"has_previous_page": false, "start_cursor": null, 
  "end_cursor": null, "has_next_page": false},
  "nodes": []},
  "blocked_by_viewer": false,
  "followed_by_viewer": false, "is_verified": false,
  "id": "3409891", "biography": null, "external_url": null}
}

如何将“profile_pic_url_hd”作为字符串回显?

2 个答案:

答案 0 :(得分:0)

使用json_decodetrue作为第二个参数,您将获得一个关联数组,但不能使用printecho数组。 试试var_dumpprint_r

$json_string = file_get_contents('https://www.instagram.com/username/?__a=1');
$parsed_json = json_decode($json_string, true);
print_r($parsed_json);
var_dump($parsed_json);

还要确保网址正确无误。

答案 1 :(得分:0)

确保检查错误/异常。在下面我假设用户'和' profile_pic_url'存在于返回的json响应中(返回的json响应是哈希的哈希)。

typedef struct Lists{
        int x;
        struct Lists *next;
}List;    

响应:

int main(int argc, char *argv[]){
        if(argc<2){ 
                printf("Error");
                return 0;
        }       
        List *root;
        int count = 0;
        root =malloc(sizeof(List)*atoi(argv[2])); 
        srand(atoi(argv[1]));
        while(count<atoi(argv[2])){
                int randomNumber = rand() % atoi(argv[3]);
                printf("Random Number is: %d",randomNumber);
                insertNodeSorted(root,randomNumber);
                count++;
        }

        return 0;
}