-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");
7
  • I've tried: Undefined Commented Dec 22, 2014 at 2:20
  • In that case you're doing something wrong. Show exactly what you're doing. Commented Dec 22, 2014 at 2:21
  • Ok, I'll edit with the whole code. Commented Dec 22, 2014 at 2:22
  • Check the spelling of summonner. This looks incorrect. Commented Dec 22, 2014 at 2:23
  • Unrelated to the question, but just a pointer: you should use encodeURIComponent on the summoner variable and api key as you insert them into the API URL. Commented Dec 22, 2014 at 2:23

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

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");

7 Comments

but console.log(summoner[summoner].id); gots me undefined :(
and console.log(summoner[summoner]?
if it displays the structure than yoiu0 should use this way the body: summoner[summoner]=JSON.parse(body.player);
if you use this way: summoner[summoner]=JSON.parse(body); console.log(summoner[summoner].player.id;?
summoner[summoner]=JSON.parse(body); console.log(summoner[summoner].summoner.id); TypeError: Cannot read property 'summoner' of undefined
|
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"]

1 Comment

Thank you! I've been looking forever for a solution to this, I don't know how I didn't think of that!
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.

1 Comment

The response returns JSON object. developer.riotgames.com/docs/getting-started
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);
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.