-1

I'm trying to write a library for Riot (League of legends API) in NodeJS and I have the following problem.

I'm doing:

function getSummonerProfile(sum) {
    var summoner = sum.replace(/\s+/g, '');

    request("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/"
            + summoner + "?api_key=" + api_key, function(error, response, body) {
        console.log(body);
    });
}

getSummonerProfile("some player");

the console shows:

{
    "player": {
        "id": 37842773,
        "name": "player",
        "profileIconId": 548,
        "summonerLevel": 30,
        "revisionDate": 1368783726000
    }
}

Now "player" is a variable (function parameter); how can I access the data? to get for example, only the id.

body.summonner and body["player"] throw undefined.

EDIT 1 (complete code):

var request = require("request");
var api_key = 'example';

function getSummonerProfile(sum) {
    var summoner = sum.replace(/\s+/g, '');

    request("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/"
            + summoner + "?api_key=" + api_key, function(error, response, body) {
        console.log(body.summoner.id);
    });
}

getSummonerProfile("some player");
mdv
  • 925
  • 3
  • 14
  • 28

5 Answers5

3

Are you actually getting a JavaScript Object? You're probably getting a JSON string, you need to parse it back into a JavaScript Object...

var newdata = JSON.parse(body);
console.log(newdata.player.id);

Support is no worry, according to caniuse.com

1

if you have a variable like this

json_var = {
    "player": {
        "id": 37842773,
        "name": "player",
        "profileIconId": 548,
        "summonerLevel": 30,
        "revisionDate": 1368783726000
    }
}

you can do this to access the data

id = json_var["player"]["id"]
aleberguer
  • 370
  • 5
  • 13
1

Your function getSummonerProfile only sends a request, using this request function which seems to be processing the internal part (console.log) when the request return with the data from server side. So basically you should save the data instead of the console.log line. You could bind it somewhere or save to a variable. And it seems that player is the body variable's key you are looking for.

Maybe something like this:

var request = require("request");
var api_key = 'example';
var summoners = {};

function getSummonerProfile(sum) {
    var summoner = sum.replace(/\s+/g, '');

    request("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" + summoner + "?api_key=" + api_key, function(error, response, body) {
        summoners[summoner]=JSON.parse(body);
        console.log(summoners[summoner].player.id);
    });
}

getSummonerProfile("some player");
Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
  • but console.log(summoner[summoner].id); gots me undefined :( – mdv Dec 22 '14 at 02:34
  • and console.log(summoner[summoner]? – Lajos Veres Dec 22 '14 at 02:35
  • if it displays the structure than yoiu0 should use this way the body: `summoner[summoner]=JSON.parse(body.player);` – Lajos Veres Dec 22 '14 at 02:36
  • if you use this way: `summoner[summoner]=JSON.parse(body); console.log(summoner[summoner].player.id;`? – Lajos Veres Dec 22 '14 at 02:41
  • summoner[summoner]=JSON.parse(body); console.log(summoner[summoner].summoner.id); TypeError: Cannot read property 'summoner' of undefined – mdv Dec 22 '14 at 02:43
  • If I do: console.log(summoners[summoner]["rklz"].id); (rklz) is my username it works!; but if i do console.log(summoners[summoner].summoner.id); it gets me undefined. – mdv Dec 22 '14 at 02:49
  • console.log(summoners[summoner][sum.replace(/\s+/g, '')].id); did the trick, thanks for all your answers. – mdv Dec 22 '14 at 02:54
0

What you are trying to do is JSON deserialization.

If you are using javascript, your question has an answer here: deserialize from json to javascript object

If you are using another language, the principle is the same, you need to have a class that matches the fields of your JSON file and use a deserializer.

Community
  • 1
  • 1
TNTanuki
  • 59
  • 7
  • The response returns JSON object. https://developer.riotgames.com/docs/getting-started – mdv Dec 22 '14 at 02:40
0

You are getting the response as a jsonstring. To get response as js object try this:

request({url: "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" + summoner + "?api_key=" + api_key, json: true}, function(error, response, body) {
        console.log(body.player.id);
    });
}
Johan Karlsson
  • 6,419
  • 1
  • 19
  • 28