-1

I just doing with javascricpt and json. I want to get data from api in which object have some details, here i want to print details of a object named "foo.txt" have some element like size, raw_url, content etc. And I want raw_url.

How can I accomplish the following:

document.getElementById("demo").innerHTML = data.files.foo(?)['raw_url']
Json is here :-
object {url: "https://api.github.com/gists/884c8343dca5b322bf6f", forks_url: "https://api.github.com/gists/884c8343dca5b322bf6f/forks", commits_url: "https://api.github.com/gists/884c8343dca5b322bf6f/commits", id: "884c8343dca5b322bf6f", git_pull_url: "https://gist.github.com/884c8343dca5b322bf6f.git"…}
comments: 0
comments_url: "https://api.github.com/gists/884c8343dca5b322bf6f/comments"
commits_url: "https://api.github.com/gists/884c8343dca5b322bf6f/commits"
created_at: "2015-11-19T09:35:23Z"
description: "test"
files: Object
foo.txt: Object
content: "sdfasfd"
filename: "foo.txt"
language: "Text"
raw_url: "https://gist.githubusercontent.com/anonymous/884c8343dca5b322bf6f/raw/5234994707b7b85bec9c80e50cb0b7dfca050e3b/foo.txt"
size: 7
truncated: false
type: "text/plain"
Smittey
  • 2,475
  • 10
  • 28
  • 35
Dhamu
  • 129
  • 1
  • 1
  • 11
  • 1
    I hope this will help you . [How to get JSON objects value if its name contains dots?](http://stackoverflow.com/questions/2577172/how-to-get-json-objects-value-if-its-name-contains-dots) Add your mock json , to clear idea – Suresh Velusamy Nov 19 '15 at 10:03

2 Answers2

1

You just want to access the property? You don't have to access properties with the dot syntax (myObject.foo.txt), which obviously causes problems here. Just access it with square brackets:

myObject['foo.txt']

var myObject = { 'foo.txt': 'Hello World!' };
var div = document.createElement('div');
div.innerHTML = myObject['foo.txt'];
document.body.appendChild(div);
Jon Surrell
  • 9,444
  • 8
  • 48
  • 54
0

Firefox has Object.toSource() which prints object as JSON or you can install Firebug and print with console.log(object)

Vollmilchbb
  • 481
  • 6
  • 20