Accessing json output by zendframework with jquery - php

{
"code":100,
"data":{
"month":[
{
"yearText":"2011",
"months":[
{
"monthText":"6",
"days":[
{
"dayText":"13",
"cios":[
{
"status":"continues",
"start":"23:00:00",
"end":"23:59:59",
"id":12
}
],
"bois":[
{
"status":"continues",
"start":"23:30:00",
"end":"23:59:59",
"id":12
}
]
},
{
"dayText":"14",
"cios":[
{
"status":"continued",
"start":"00:00:00",
"end":"01:00:00",
"id":12
},
{
"status":"within",
"start":"11:42:14",
"end":"11:43:45",
"id":11
}
],
"bois":[
{
"status":"continued",
"start":"00:00:00",
"end":"00:30:00",
"id":12
},
{
"status":"within",
"start":"11:42:39",
"end":"11:43:33",
"id":11
}
]
}
]
}
]
}
],
"next":"\/attendance\/get-history\/2011\/07",
"previous":"\/attendance\/get-history\/2011\/05"
},
"msg":"Attendance history of John Doe on June, 2011."
}
this json output file was generated by zendframework, i am trying to access the objects inside the "months" : string through jquery, which looks like an array.
i have tried to access them like usual objects as in month.yearText with no luck, i am not doing it right of coz.
any help? please.
thanks

Try jQuery's parseJSON() function
Something like this:
var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

You need to use month[0].yearText, because month is array.

Related

AngularJS json output format

I have problem with outputting data from JSON file
{
"movie": {
"RowCount": 0,
"Result": [
{
"movieID": "124",
"moviename": "Hello"
},
{
"movieID": "123",
"moviename": "World"
}
]
}
}
I dont know why the {{}} statement wont work with this kind of JSON file. if the JSON File with another format like following
{
"discCount": 0,
"results":[
{
"disccode": "ABC123"
},
{
"disccode": "ABCD123"
}
]
}
it works perfect just by calling the {{variablename.disccode}}
can anybody help me please? thank you
From your edit, it appeared that you are resolving the wrong property. You should resolve :
defer.resolve({
data: response.movie.Result,
rowCount: response.movie.RowCount
});
Then, because the controller stores the data in $scope.movies, the following ng-repeat will be ok :
<tr data-ng-repeat="movie in movies">
<td>{{movie.ID}}</td><td>{{movie.moviename}}</td>
</tr>

Mongo Db insert element into nested mongo array

I have following document structure
{
assets : {
type : [
{
type : spread
scene : [
{
....
},
{
....
}
]
}
]
}
}
So I want to add new element in "scene" array with match condition of type "spread" in same document
How can I write Mongo Update query for the same ?
Following is example array
{
assets : {
type : [
{
type : spread
scene : [
{
....
},
{
....
}
]
}
]
}
}
Reference link
MongoDB & Meteor - Query to push into nested array does not work, no error thrown
And query which insert new element in scene array is
db.PhotostoryDesigner.update(
{
"photostoryId":"123",
"assets.type.type":"spreads"
},
{"$push": {
'assets.type.$.scene':{"$each":[
{
"scene_id": "1",
"name": "Ad hoc 1",
"spread": [
{
"spreadId": "7725",
"name": "PAGE 1-2",
"asset": []
}
]
}
]}}
}
);

Getting random output json

I trying to get a youtube channel banner from the youtube api.
But this is very weird.
I use this code:
$json1 = file_get_contents("http://gdata.youtube.com/feeds/api/partners/$channel/branding/default?key=[api-key]&alt=json");
$data1 = json_decode($json1, true);
$banner = $data1['entry']['yt$option'][15]['$t'];
echo $banner;
Now the weird thing, when i refresh te page i get everytime a new value. but sometimes it will stay one value and thats what i need but after some thime the value is changing...
This is the json code:
{
"version":"1.0",
"encoding":"UTF-8",
"entry":{
"xmlns":"http://www.w3.org/2005/Atom",
"xmlns$yt":"http://gdata.youtube.com/schemas/2007",
"id":{
"$t":"http://gdata.youtube.com/feeds/api/partners/rebasenetwork/branding/default"
},
"published":{
"$t":"2014-02-28T22:08:37.000Z"
},
"updated":{
"$t":"2014-02-28T22:08:37.000Z"
},
"category":[
{
"scheme":"http://schemas.google.com/g/2005#kind",
"term":"http://gdata.youtube.com/schemas/2007#branding"
}
],
"link":[
{
"rel":"self",
"type":"application/atom+xml",
"href":"http://gdata.youtube.com/feeds/api/partners/rebasenetwork/branding/default"
},
{
"rel":"edit",
"type":"application/atom+xml",
"href":"http://gdata.youtube.com/feeds/api/partners/rebasenetwork/branding/default"
}
],
"yt$option":[
{
"$t":"0",
"name":"channel.banner.image_height.int"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_mobile_banner_hd.jpg?v=52d6ad2b",
"name":"channel.banner.mobile.hd.image.url"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_tablet_banner.jpg?v=52d6ad2b",
"name":"channel.banner.tablet.medium.image.url"
},
{
"$t":"https://lh6.googleusercontent.com/-6xCc8OBY5Qk/UtatKX2WY3I/AAAAAAAAAAY/6-vpTOgd0yU/w854-fcrop64=1,00000000ffffffff/channels4_banner.jpg",
"name":"channel.banner.tv.low.image.url"
},
{
"$t":"https://lh6.googleusercontent.com/-6xCc8OBY5Qk/UtatKX2WY3I/AAAAAAAAAAY/6-vpTOgd0yU/w1280-fcrop64=1,00000000ffffffff/channels4_banner.jpg",
"name":"channel.banner.tv.medium.image.url"
},
{
"$t":"#000000",
"name":"channel.global.color"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_banner.jpg?v=52d6ad2b",
"name":"channel.banner.image.url"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_mobile_banner_low.jpg?v=52d6ad2b",
"name":"channel.banner.mobile.low.image.url"
},
{
"$t":"True",
"name":"channel.modules.show_comments.bool"
},
{
"$t":"https://lh6.googleusercontent.com/-6xCc8OBY5Qk/UtatKX2WY3I/AAAAAAAAAAY/6-vpTOgd0yU/w2120-fcrop64=1,00000000ffffffff/channels4_banner.jpg",
"name":"channel.banner.tv.image.url"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_tablet_banner_hd.jpg?v=52d6ad2b",
"name":"channel.banner.tablet.hd.image.url"
},
{
"$t":"Welcome to our network Channel. We help Youtubers to earn money and grow with their channel. Being famous on youtube is one of the things you dream about, isn't it? Let us help make your dreams true!\n\nWith us you can choose your own partnership, You also select what you want to earn from your youtube channel! Are you ready to start growing? Partner Now!",
"name":"channel.global.description.string"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_mobile_banner_medium_hd.jpg?v=52d6ad2b",
"name":"channel.banner.mobile.medium_hd.image.url"
},
{
"$t":"https://lh6.googleusercontent.com/-6xCc8OBY5Qk/UtatKX2WY3I/AAAAAAAAAAY/6-vpTOgd0yU/w1920-fcrop64=1,00000000ffffffff/channels4_banner.jpg",
"name":"channel.banner.tv.high.image.url"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_mobile_banner.jpg?v=52d6ad2b",
"name":"channel.banner.mobile.medium.image.url"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_mobile_banner.jpg?v=52d6ad2b",
"name":"channel.banner.mobile.image.url"
},
{
"$t":"True",
"name":"channel.related_channels.show.bool"
},
{
"$t":"KQvIpdiWrSI",
"name":"channel.unsubscribed_trailer.video_id.string"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_tablet_banner_extra_hd.jpg?v=52d6ad2b",
"name":"channel.banner.tablet.extra_hd.image.url"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_tablet_banner_low.jpg?v=52d6ad2b",
"name":"channel.banner.tablet.low.image.url"
},
{
"$t":"True",
"name":"channel.modules.moderate_comments.bool"
},
{
"$t":"Network partner partnership rebase network pewdiepie",
"name":"channel.global.keywords.string"
},
{
"$t":"Rebase Network",
"name":"channel.global.title.string"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_mobile_banner_extra_hd.jpg?v=52d6ad2b",
"name":"channel.banner.mobile.extra_hd.image.url"
}
]
}
}
This is the one i need(just the url):
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_tablet_banner_extra_hd.jpg?v=52d6ad2b",
"name":"channel.banner.tablet.extra_hd.image.url"
},
Can someone help me please? i really don't understand why it's changing the value every refresh..
You can loop through the results and print out the one where the name matches channel.banner.mobile.extra_hd.image.url
$data1 = json_decode($json1, true);
foreach ($data1['entry']['yt$option'] AS $banner) {
if ($banner['name'] == 'channel.banner.mobile.extra_hd.image.url') {
print $banner['$t'];
}
}

How do I get the field named body from this json code via php?

I have been trying everything and I just need help. Thanks
I need the body and mobileNumber values and I need each set of values separately in cdv format.
Example
"Hello little cat!" , "6502642742"
"I am home" , "6502642742"
I have tried a few things the last was
$son = json_decode($json);
echo $son->sessions[0]->message[0]->body;
{
"response":null,
"sessions":[
{
"message":[
{
"to":null,
"body":"Hello little cat!",
},
"mobileNumber":"6502642742",
},
{
"to":null,
"body":"I am home",
},
"mobileNumber":"6502642742",
}
],
"session":"26e2fa57-5d42-4678-bda5-6b233d05b754c:257539402",
"conversation":[
{
"lastContactFirstName":"",
"lastContactLastName":"",
}
]
}
],
"success":true
}

how to read multi-levels json format using jquery

i have a json format like
[
{
id: 15,
diemdung: "a"
},
{
id: "16",
diemdung: "b",
khoangcach: "300",
pho: "c",
da: [
{
lancan: "d",
kc: "333"
},
{
lancan: "e",
kc: "322"
}
]
},
...
]
i using php like print json_encode($rows);
and i try to read it at client using jquery like
$.getJSON(url,function(json){
$.each(json,function(key, val){
$.each(this.da,function(){
alert(this.kc);
});
});
});
but it's not working. How i do that? thanks
If your code is otherwise working, you may be getting the following error:
TypeError: obj is undefined
This is because the first object in the outer array does not have a value for the "da" property. Before you try to loop over the array held by the "da" property, you should check if it exists.
Try:
$.getJSON(url,function(json){
$.each(json,function(){
if (this.da) {
$.each(this.da,function(){
alert(this.kc);
});
}
});
});
$.each(json, function(arrayID, arrayVal) {
//alert(arrayVal.id);
$.each(arrayVal.da, function(daKey,daData) {
alert(daData.kc);
});
});
Heres my same SO question a while ago for further code
JQuery $.each() JSON array object iteration
edit to rename some variables to make clearer
For n level hierarchy
var str = '{"countries":[{"name":"USA","grandfathers":[{"gFName":"Steve","grandfathersKid":[{"gFKName": "Linda","kid": [{"name": "Steve JR", "friends": [{"name": "Kriss|John|Martin|Steven"}]}]}]}]}]}';
var obj = jQuery.parseJSON(str);
parseJsonString(obj);
Function :
function parseJsonString(data){
$.each(data, function(index, val){
if($.type(val) == 'object' || $.type(val) == 'array'){
product.parseJsonString(val);
}else{
alert(index + ' - ' + val);
}
});
}

Categories