我想通过我的PHP文件启动节点js应用程序。 之后,我想将结果保存在像$ result
这样的变量中数据看起来像这样:
[{ id: 'ID', displayName: 'DisplayedName' },
{ id: 'ID', displayName: 'DisplayedName' },
...]
我不知道如何使用它。
我的问题:
这是其中一项功能
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 ???
答案 0 :(得分:0)
我刚刚将{"leaderboard":
添加到biggining并}
添加到最后。
现在它是一个带有数组的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'];
我希望这会有所帮助。