Reputation: 414
I'm working on a page that musts shows data received from an external page (this page is external to my domain and it isn't under my control). This external page replies with some JSON data in the following structure:
{
"definition" : {
"accountID" : 19943,
"profileID" : "Z3WIPu86X77",
"ID" : "oOEWQj3sUo6",
"name" : "Site Pages",
"description" : "A10_RPT_Pages_SD",
"language" : null,
"timezone" : "UTC 1",
"dimensions" : [
{
"ID" : "pages",
"name" : "Page"
}
],
"measures" : [
{
"name" : "PageViews",
"ID" : "CAoNYspmFb5",
"columnID" : 1,
"measureFormatType" : null
}
]
},
"data" : [
{
"period" : "Month",
"start_date" : "2014-03",
"end_date" : "2014-04",
"attributes" : {
},
"measures" : {
"PageViews" : 281
},
"SubRows" : {
}
}
]
}
What that I want to do is to get back the specific data "data.measures.PageViews" to insert it inside my HTML code.
I have defined the following javascript in the html head:
<script type="text/javascript">
function parseResult(myData)
{
alert(myData.data);
}
</script>
that I use making an address callback to the external link in the following manner inside the body:
<script type="text/javascript" src="https://external-link&callback=parseResult"></script>
If I do it the alert shows me "Object: object".
However if I try to do alert(myData.data.measures);
it shows me a blank alert and if I try to do alert(myData.data.measures.PageViews);
it doesn't show the alert and gives me an error on the firebug console.
I have read some jsonp guide but I don't understand a lot of things. However after that I want to put Pageviews in a variable to call this inside the html where I prefer.
Upvotes: 0
Views: 151
Reputation: 4216
With jQuery, you can use $.getJSON
to do cross-site JSON fetching:
$.getJSON("https://external-link&callback=parseResult",
function(myData) {
alert(myData.data);
});
Upvotes: 0
Reputation: 91
Look at your JSON, myData.data
is an array so you cannot access it like this myData.data.measures
.
Try myData.data[1]
Upvotes: 0