0

Hi all I'm new to json and I am trying to get the price of gold 3rd level contents->MyFeed->XAUCAD->bid H get this feed from php proxy can someone help be in how to get the gold price

$(document).ready(function () {
    $.getJSON("http://feed.php",
        function (data) {
            alert(data.gold);
        });
});

JSON

{
    "headers": {
        "Cache-Control": "private",
        "Content-Type": "application/json; charset=utf-8",
        "Server": "Microsoft-IIS/7.0",
        "X-AspNet-Version": "4.0.30319",
        "X-Powered-By": "ASP.NET",
        "X-Served-By": "",
        "Date": "Wed, 22 Jan 2014 06:35:06 GMT",
        "Content-Length": "200"
    },
    "status": {},
    "contents": {
        "MyFeed": {
            "@Provider": "FastMarkets",
            "XAUCAD": {
                "@name": "Gold Canadian $",
                "bid_time": "20140122063506",
                "bid": "1360.44"
            },
            "XAGCAD": {
                "@name": "Silver Canadian $",
                "bid_time": "20140122063507",
                "bid": "21.76"
            }
        }
    }
}
Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111

3 Answers3

2
console.log(data.contents.MyFeed.XAUCAD.bid);
Ohgodwhy
  • 47,967
  • 9
  • 74
  • 103
1

I have considered that data = JSON file that you have mentioned

$(document).ready(function () {
    $.getJSON("http://feed.php",
        function (data) {
//This should open alert dialog containing gold bid
            alert(data.contents.MyFeed.XAUCAD.bid);
    });
});
Tushar Thakur
  • 916
  • 3
  • 15
  • 32
0

You can get this way

var a ={
    "headers": {
        "Cache-Control": "private",
        "Content-Type": "application/json; charset=utf-8",
        "Server": "Microsoft-IIS/7.0",
        "X-AspNet-Version": "4.0.30319",
        "X-Powered-By": "ASP.NET",
        "X-Served-By": "",
        "Date": "Wed, 22 Jan 2014 06:35:06 GMT",
        "Content-Length": "200"
    },
    "status": {
    },
    "contents": {
        "MyFeed": {
            "@Provider": "FastMarkets",
            "XAUCAD": {
                "name": "Gold Canadian $",
                "bid_time": "20140122063506",
                "bid": "1360.44"
            },
            "XAGCAD": {
                "name": "Silver Canadian $",
                "bid_time": "20140122063507",
                "bid": "21.76"
            }
        }
    }
}

$(document).ready(function(){
    alert(a.contents.MyFeed.XAUCAD.bid);
});

Fiddle Demo

Chirag Vidani
  • 2,349
  • 17
  • 24