繁体   English   中英

我从API获取信息,但无法使用它

[英]I am getting information from an API but i am not able to use it

我想通过我的PHP文件启动Node js应用程序。 之后,我想将结果保存在$ result之类的变量中

数据如下所示:

[{ id: 'ID', displayName: 'DisplayedName' },
{ id: 'ID', displayName: 'DisplayedName' },
 ...]

而且我不知道该如何使用。

我的问题:

  1. 是否可以将其转换为JSON文件,如果可以的话?
  2. 有没有办法使用数据(从中提取信息),如果可以的话?

编辑:添加了功能

这是功能之一

static get Sth1() { return "sth1" }
static get Sth2() { return "sth2" }
static get Sth3() { return "sth3" }    

getMeSth(platform,type) {
    return new Promise((resolve, reject) => {
        if (!(platform == "pc" || platform == "ps4" || platform == "xb1")) {
            reject("Please precise a good platform: ps4/xb1/pc")
        }

        if(!(type == this.constructor.Sth1 || type == this.constructor.Sth2 || type == this.constructor.Sth3)) {
            reject("Please precise a good type test.Sth1/test.Sth2/test.Sth3")
        }

        request({
            url: EndPoint.leaderBoardScore(platform,type),
            headers: {
                Authorization: "author " + this.access_token
            },
            method: "POST",
            json: true
        }).then(leaderboard => {
            leaderboard = leaderboard.entries

            leaderboard.forEach(i => {
                i.accountId = i.accountId.replace(/-/g,'')
            })

            request({
                url: EndPoint.displayNameFromIds(leaderboard.map(i => i.accountId)),
                headers: {
                    Authorization: "author " + this.access_token
                },
                method: "GET",
                json: true
            }).then(displayNames => {
                leaderboard.forEach(i => {
                    i.displayName = displayNames.find(ii => ii.id === i.accountId).displayName
                })

                resolve(JSON.stringify(leaderboard))
            })
            .catch(err => {
                reject(err)
            })

        })
        .catch(err => {
            reject(err)
        })
    })
}

编辑:添加结果

现在,我将JSON.stringify()内置到应用程序中,就得到了此数据

[{"accountId":"ID1","value":913,"rank":1,"displayName":"Name1"},{"accountId":"ID2","value":892,"rank":2,"displayName":"Name2"},{"accountId":"ID3","value":868,"rank":3,"displayName":"Name3"},...]

我如何提取重要信息,例如Value / Rank / DisplayName ???

解:

我刚刚在结尾处添加了{"leaderboard": ,最后添加了} 现在,它是一个带有数组的JSON对象,其中包含许多其他对象。

$output = "{\"leaderboard\"" . ":" . exec('node /path/to/node/js/file/doSth.js &') . "}";

现在我的$output

{"leaderboard":[{"accountId":"ID1","value":913,"rank":1,"displayName":"Name1"},{"accountId":"ID2","value":892,"rank":2,"displayName":"Name2"},{"accountId":"ID3","value":868,"rank":3,"displayName":"Name3"},...]}

我可以用提取我的信息

$DName = json_decode($output, true);
echo $DName['leaderboard'][0]['displayName'];

我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM