简体   繁体   中英

js access to object JSON from url

in some case i want to show a specific object of JSON file which is load from url in js:

 <div id="me2"></div> <script> function Get(yourUrl){ var Httpreq = new XMLHttpRequest(); // a new request Httpreq.open("GET",yourUrl,false); Httpreq.send(null); return Httpreq.responseText; } var json_obj = JSON.parse(Get('https://www.instagram.com/barkobco/?__a=1')); document.getElementById("me2").innerHTML = json_obj.graphql.user.edge_follow; </script> </body> </html>

but the output is: [object Object]

why?

the correct object value is: 25

If you are trying to get the count, you'll have to go 1 more level:

 <div id="me2"></div> <script> function Get(yourUrl){ var Httpreq = new XMLHttpRequest(); // a new request Httpreq.open("GET",yourUrl,false); Httpreq.send(null); return Httpreq.responseText; } var json_obj = JSON.parse(Get('https://www.instagram.com/barkobco/?__a=1')); document.getElementById("me2").innerHTML = json_obj.graphql.user.edge_follow.count; </script> </body> </html>

That's because json_obj.graphql.user.edge_follow is still an object you need to find .count of it.

 <div id="me2"></div> <script> function Get(yourUrl){ var Httpreq = new XMLHttpRequest(); // a new request Httpreq.open("GET",yourUrl,false); Httpreq.send(null); return Httpreq.responseText; } var json_obj = JSON.parse(Get('https://www.instagram.com/barkobco/?__a=1')); console.log(json_obj) document.getElementById("me2").innerHTML = json_obj.graphql.user.edge_follow.count; </script> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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