How does a Json object return?

Asked

Viewed 837 times

-2

I just modified an example of HTML with Json but it is an array type and apparently what I want to use is an object, I believe this is the problem. json contains this information:

{"riotschmick":{"id":585897,"name":"Riotschmick","profileIconId":956,"summonerLevel":30,"revisionDate":1449128440000}}

I believe there’s something missing in the code to work Here is the example:

<!DOCTYPE html>
<html>
<body>


<h1>Meu Projeto</h1>
<div id="id01"></div>
<div id="id02"></div>
<script>
var xmlhttp = new XMLHttpRequest();

var url = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/RiotSchmick?api_key=a15c56d1-fdd7-4da2-ad9c-0f1a6585ac1b";

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
    var arr = JSON.parse(response);
    var out = "<h1>";
    out += arr.id + arr.name + arr.summonerLevel;
    out += "</h1>";
    document.getElementById("id01").innerHTML = out;
}

</script>

</body>
</html>

  • What do you mean, "type of JSON"? I read your question and did not understand.

  • json files have a unique structure, if you change it is no longer json. Explain it better, talk about another file .json with data other than the current ?

  • I’ve found thank you all!

2 answers

2


I figured out the problem. This Json is a complex object, to work I did so:

    out += arr.riotschmick.id + arr.riotschmick.name + arr.riotschmick.summonerLevel;

2

The second "JSON" you are using is not a list, so you do not need the index to access the properties of the object.

out += arr.id + arr.name + arr.summonerLevel;

One-night stand in this Javascript guide that explains what it is and how to work with objects.

Edited: AP changed question, JSON changed.

What is an object?

object is an independent entity, with properties and type.

How to access the properties of my object?

nameObject.propertyName

How do I know which property has my object?

If you do not know what properties your object has in Javascript, one option is to print it in the browser console (console.log('')), this way you can see all the properties of your object and identify if it is just an object or a collection of objects.

What is a collection of objects?

It is an Array of objects, ie several objects grouped in a single location.

In the link I mentioned at the beginning of the reply there is a good explanation detailed and clear about what an object is, I suggest you read and try understand, if you have any doubts, look in the community and if not find an answer you can post a new question whenever you judge necessary.

var obj = {
  "riotschmick": {
    "id": 585897,
    "name": "RiotSchmick",
    "profileIconId": 956,
    "summonerLe‌​vel": 30,
    "revisionDate": 1449128440000
  }
};

console.log(obj);
console.log(obj.riotschmick);
console.log(obj.riotschmick.id);
console.log(obj.riotschmick.name);

  • doesn’t work...json is: {"riotschmick":{"id":585897,"name":"Riotschmick","profileIconId":956,"summonerLevel":30,"revisionDate":1449128440000}} but I can’t catch it msm like this

  • @Johnner changed the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.