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
  • 795,719
  • 175
  • 1,089
  • 1,143

3 Answers3

2
console.log(data.contents.MyFeed.XAUCAD.bid);
Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
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
  • 956
  • 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,519
  • 18
  • 26