0

When I try to print my object it gives me no response.

var steaminv = JSON.parse("http://steamcommunity.com/id/pootel/inventory/json/730/2.json");
document.write(steaminv);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
bbousq
  • 615
  • 1
  • 5
  • 13
  • 2
    You're trying to parse the string `"http://steamcommunity.com/id/pootel/inventory/json/730/2.json"`, you never actually requested the JSON file. – Jeff Dec 13 '15 at 07:16
  • @Jyeon how would i request the JSON file? – bbousq Dec 13 '15 at 07:18
  • 1
    Where did you get the idea from that it would be possible to pass a URL to `JSON.parse`? – Felix Kling Dec 13 '15 at 07:25
  • @FelixKling from php – bbousq Dec 13 '15 at 07:30
  • 1
    PHP doesn't have `JSON.parse`. The equivalent is `json_decode`, which doesn't accept a URI either. Also PHP and JavaScript are different languages. If you are trying out an unfamiliar method, [read its documentation first!](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) – Felix Kling Dec 13 '15 at 07:32

1 Answers1

2

You should request the file first and then parse the content of the file.

JSON.parse requires a json object encoded as string. It does not request the file for you.

If you are using node, you can use request module. If you are using javascript on browser, you can use jQuery and do an ajax call to get the content of the file.

Please take a look at this question: How do I receive a JSON file using AJAX and parse it using javascript?

Just to give you an idea what JSON.parse does:

var str = '{"name":"Amir","age":25}';
var obj = JSON.parse(str);
console.log(obj.name);
console.log(obj.age);
Community
  • 1
  • 1
Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52