AngularJS json output format - php

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>

Related

How to get values from json object to php array

I am writing a php file for an application that allows the user to print a project report (with fpdf library).
The aim is to get datas from a db and then to build the PDF file dynamically.
The datas are stored in json.
It is ok for almost all datas, but there are some that I can't reach.
Here is an example :
First of all I already did this :
$Array['roof_coordinates'] = json_decode($Array['roof_coordinates'], false);
To get the number "nb" of "A523500" I'm doing like that :
$Array['roof_coordinates']->results->packingList->total->A523500->nb;
What am I doing wrong ?
"roofCoordinates": {
"results": {
"packingList": {
"total": {
"A523500": {
"ref": "STRA523500",
"nb": 16
},
"A523120": {
"ref": "STRA523120",
"nb": 0
},
"A522100": {
"ref": "STRA522100",
"nb": 8
},
},
}
},
}
And I tried to pass "true" to json_decode to convert objects to associative array but it doesn't seems to work...
Any help will be great !
Thank you in advance ;)
Make sure you are accessing the resulting data structure correctly, it should work whether you decode to an array or an object.
<?php
$json = <<<END
{
"roofCoordinates": {
"results": {
"packingList": {
"total": {
"A523500": {
"ref": "STRA523500",
"nb": 16
},
"A523120": {
"ref": "STRA523120",
"nb": 0
},
"A522100": {
"ref": "STRA522100",
"nb": 8
}
}
}
}
}
}
END;
$arrayResults = json_decode($json, true);
$nbFromArray = $arrayResults['roofCoordinates']['results']['packingList']['total']['A523500']['nb'];
$stdClassResults = json_decode($json);
$nbFromStdClass = $stdClassResults->roofCoordinates->results->packingList->total->A523500->nb;
assert($nbFromArray==16, 'Value should equal 16');
assert($nbFromArray==$nbFromStdClass, 'Values from either json_decode method should be equal');
echo 'From array: '.$nbFromArray.PHP_EOL;
echo 'From stdClass: '.$nbFromStdClass.PHP_EOL;

How do I save an image file from json string which contains the image file?

This is a bit confusing. I use w2ui framework as my user interface and it sends JSON string when it posts a form that contains a file input. Now the JSON string looks like this:
{
"cmd":"save",
"recid":0,
"record":
{
"id":"2",
"image":
[
{
"name":"test.jpg",
"type":"image/jpeg",
"modified":"2016-04-03T15:54:12.638Z",
"size":31216,
"content":"very_long_content_string"
}
]
},
"id":"2",
"image":
[
{
"name":"test.jpg",
"type":"image/jpeg",
"modified":"2016-04-03T15:54:12.638Z",
"size":31216,
"content":"very_long_content_string"
}
]
}
I don't know why the information is redundant. The question is how do I somehow save the file inside the image array into my local disk?
Parse the JSON and use:
<?php
$base64string = very_long_content_string;
file_put_contents('img.png', base64_decode($base64string));
?>

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
}

Acessing a JSON Object using Javascript

I have the following JSON object
({
"codes": [ {
"code": {
"redeemed": "true", "retailer": "R1", "code": "ab11845b55b3ef1c3f137a35dbdfb270"
}
}, {
"code": {
"redeemed": "false", "code": "48c02f7bd35271de0aa215209b0a390f", "message": "code already used"
}
} ]
});
for (var code in data.codes) {
console.log(code) // prints 0
console.log(code[0].id) // prints 0
}
How can I access the inner objects separately ?
To output each individual "code" object:
// First, assign your data to a variable:
var data = ({
"codes": [ {
"code": {
"redeemed": "true", "retailer": "R1", "code": "ab11845b55b3ef1c3f137a35dbdfb270"
}
}, {
"code": {
"redeemed": "false", "code": "48c02f7bd35271de0aa215209b0a390f", "message": "code already used"
}
} ]
});
// (I use the "c" temprary variable here because it's shorter)
var c = data.codes;
// Loop through all the codes.
for (var i = 0; i < c.length; i++) {
// Log the specific code at [i].
console.log(c[i].code); // Output a "code" object.
}
You can then use:
c[i].code.redeemed;
c[i].code.retailer;
c[i].code.code;
c[i].code.message;
These will however return undefined when they're not set as a property of the specific "code" object.
Working example
assuming what you had added:
window.json_obj = ({"codes":[{"code":{"redeemed":"true","retailer":"R1","code":"ab11845b55b3ef1c3f137a35dbdfb270"}},{"code":{"redeemed":"false","code":"48c02f7bd35271de0aa215209b0a390f","message":"code already used"}}]});
console.log(json_obj.codes[0]['code']['code']);
// or
// console.log(json_obj.codes[0].code.code);
jsFiddle demo
console.log( obj.codes[0].code.retailer );
console.log( obj.codes[1].code.message );
Outputs
R1
code already used
None of the objects have an id property.
First:
Your JSON should not be in the parens, if you have no reason for it to be:
(<JSON>);
If that's all you're doing, it isn't going to let you access it.
You need to assign it to a variable...
var data = { codes : [ { code : "...", ...}, ... ] };
Once you do that, you have access like:
data.codes[i].code.code;
data.codes[i].code.redeemed;
PS: if it's not a full string
'{ "codes":[{"code":"...",...},...]}'
then it's not a JSON object, it's a JavaScript object.
Yes, that sounds picky.
But it all comes down to if you need to parse it and how, before you can use it properly.
the for (var stuff in whatever) returns you the index of the object.
So basically, to access your object you have to do
console.log(data.codes[code]);
console.log(data.codes[code].id);
and please format your json in a readable way next time, hard to read here :)

Accessing json output by zendframework with jquery

{
"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.

Categories