How to customize json response? - php

I request a JSON query and get the output, I want to parse that output and show it in tabular form also want to insert it into database.
I performed following php code to get array representation of JSON data.
echo '<pre>';
print_r( json_decode( $result ) );
echo '</pre>';
and I get following output:
stdClass Object
(
[request] => stdClass Object
(
[Target] => Affiliate_Report
[Format] => json
[Service] => HasOffers
[Version] => 3
[Method] => getConversions
[api_key] =>
[NetworkId] =>
[limit] => 2
[fields] => Array
(
[0] => Offer.name
[1] => Browser.display_name
[2] => Stat.payout
[3] => Stat.sale_amount
[4] => Stat.status
[5] => Stat.datetime
[6] => Stat.ip
[7] => Stat.ad_id
[8] => Stat.affiliate_info1
)
)
[response] => stdClass Object
(
[status] => 1
[httpStatus] => 200
[data] => stdClass Object
(
[page] => 1
[current] => 2
[count] => 81
[pageCount] => 41
[data] => Array
(
[0] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Firefox
)
[Stat] => stdClass Object
(
[payout] => 150.00000
[sale_amount] => 0.00000
[status] => approved
[datetime] => 2014-05-20 22:20:05
[ip] => 27.0.50.82
[ad_id] => 102fa12e74df6018e502d8e152adb2
[affiliate_info1] =>
)
)
[1] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Firefox
)
[Stat] => stdClass Object
(
[payout] => 150.00000
[sale_amount] => 53.00000
[status] => rejected
[datetime] => 2014-03-30 13:14:50
[ip] => 27.0.51.145
[ad_id] => 102be1d682ac9b2e9ee8e14dd1aeca
[affiliate_info1] =>
)
)
)
[dbSource] => branddb
)
[errors] => Array
(
)
[errorMessage] =>
)
)
I want to display above data into tabular form.
Code used by me:
$result = file_get_contents($base);
$obj = json_decode($result, true);
<?php foreach ($obj['response'] as $licenseElement) :?>
<tr>
<td><?php echo $licenseElement->data->Offer->name; ?></td>
<td><?php echo $licenseElement->Stat->payout; ?></td>
<td><?php echo $licenseElement->Stat->sale_amount; ?></td>
<td><?php echo $licenseElement->Stat->datetime; ?></td>
</tr>
<?php endforeach; ?>
This code returns me an error Trying to get property of non-object everywhere at echo syntax.
Please help me to parse the above json output and display it in proper tabular format.

you are iterating over wrong object, you need to loop over $obj->response->data->data object to get what you are seeking
<?php foreach ($obj->response->data->data as $licenseElement) :?>
<tr>
<td><?php echo $licenseElement->Offer->name; ?></td>
<td><?php echo $licenseElement->Stat->payout; ?></td>
<td><?php echo $licenseElement->Stat->sale_amount; ?></td>
<td><?php echo $licenseElement->Stat->datetime; ?></td>
</tr>
<?php endforeach; ?>

You are not getting to the right level in the object hierarchy. Try with
foreach($obj->response->data->data as $licenseElement) { ... }

$result = file_get_contents($base);
$obj = json_decode($result, true);
<?php foreach ($obj['response']->data->data as $licenseElement) :?>
<tr>
<td><?php echo $licenseElement->data->Offer->name; ?></td>
<td><?php echo $licenseElement->Stat->payout; ?></td>
<td><?php echo $licenseElement->Stat->sale_amount; ?></td>
<td><?php echo $licenseElement->Stat->datetime; ?></td>
</tr>
<?php endforeach; ?>
This code should work.
What you are trying to do is that you have considered the response as an array but the print_r functions lets us know wether your data is an array or an object.
It says StdClass Object thats why we need the "->" sign to refer to it.
All i have changed is the foreach($obj['response']->data->data as $licenseElement)
We also need to check the levels , here we are 3 levels in. :)
Hope this helps.

Related

how i can access to this values? php

I want print: Id, Nombre_del_paciente__c, Fecha_de_la_cita__c and Hora_de_la_cita__c
This is print_r($response); result:
Object (
[queryLocator] => [done] => 1 [records] => Array (
[0] => SObject Object (
[type] =>
Cita__c [fields] => stdClass Object
(
[Nombre_del_paciente__c] => 0030O000021cPBuQAM
[Fecha_de_la_cita__c] => 2017-11-28
[Hora_de_la_cita__c] => 15:30
)
[Id] => a000O00000tmZH6QAM
)
) [size] => 1
)
I do this and I can acces to ID value... but i cant acceso to other values:
<table>
<tr>
<th>ID </th>
<th>Nombre_del_paciente__c</th>
<th>Fecha_de_la_cita__c</th>
<th>Hora_de_la_cita__c</th>
</tr>
<?php
foreach ($response->records as $record) {
echo '<tr>
<td>'.$record->Id.'</td>
<td></td>
<td></td>
<td></td>
</tr>';
}
?>
</table>
Im try to do:
$record->type->Nombre_del_paciente__c
$record->Cita__c ->Nombre_del_paciente__c
$record->Cita__c['fields'] ->Nombre_del_paciente__c
but i cant acces to values
Your key 'type' is not a object array. you can try this-
$record->type['fields']->Nombre_del_paciente__c
$record->type['fields']->Fecha_de_la_cita__c
$record->type['fields']->Hora_de_la_cita__c

Access variables from print_r in php

I got a print_r($result) output as follows:
stdClass Object (
[balance] => 998
[batch_id] => 243941208
[cost] => 1
[num_messages] => 1
[message] => stdClass Object (
[num_parts] => 1
[sender] => TMTLCO
[content] => #U0D070D240D4D00200D120D300D4100200D1F0D460D380D4D0D310D4D0D310D4D002000200D060D230D4D
)
[receipt_url] =>
[custom] =>
[messages] => Array (
[0] => stdClass Object (
[id] => 117250619
[recipient] => XXXXXXXX
)
)
[status] => success
)
I could echo the following and get correct output:
echo $result->balance;
echo $result->num_messages;
But the following is not working??
echo $result->message->sender;
echo $result->messages->recipient;
echo $result->messages->status;
What am i doing wrong ??
Miss the 0 which is an array. For accessing recipient you need to add one more Dimension into your $result array.
Messages has a one more array which you miss to navigate. So try with this,
echo $result->message->sender;
echo $result->messages[0]->recipient;
echo $result->status;

how to manage json data in php

I called JSON data and got following output:
stdClass Object
(
[request] => stdClass Object
(
[Target] => Affiliate_Report
[Format] => json
[Service] => HasOffers
[Version] => 3
[Method] => getConversions
[api_key] => my_key
[NetworkId] => icubes
[fields] => Array
(
[0] => Offer.name
[1] => Browser.display_name
[2] => Stat.payout
[3] => Stat.sale_amount
[4] => Stat.status
[5] => Stat.datetime
[6] => Stat.ip
[7] => Stat.ad_id
[8] => Stat.affiliate_info1
)
)
[response] => stdClass Object
(
[status] => 1
[httpStatus] => 200
[data] => stdClass Object
(
[page] => 1
[current] => 100
[count] => 75
[pageCount] => 1
[data] => Array
(
[0] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Chrome
)
[Stat] => stdClass Object
(
[payout] => 150.00000
[sale_amount] => 954.00000
[status] => rejected
[datetime] => 2014-03-31 19:49:50
[ip] => 103.226.84.249
[ad_id] => 102ecc5fe230883c195d8a0e84ef7f
[affiliate_info1] =>
)
)
[1] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Firefox
)
[Stat] => stdClass Object
(
[payout] => 270.00000
[sale_amount] => 545.00000
[status] => approved
[datetime] => 2014-04-18 12:00:20
[ip] => 27.0.51.119
[ad_id] => 10256740541d68b117955aa58529a6
[affiliate_info1] =>
)
)
I want to display that data in tabular form using table tag and also want to insert that data into a MySQL database.
But I dont understand the array format of data.
My code:
$result = file_get_contents($base);
$obj = json_decode($result);
echo"<table>";
foreach($obj as $item) {
echo"
<tr>
<td>$item['Offer.name']</td>
<td>$item['Browser.display_name']</td>
<td>$item['Stat.payout']</td>
<td>$item['Stat.sale_amount']</td>
<td>$item['Stat.datetime']</td>
<td>$item['Stat.ip']</td>
</tr>
";
}
echo"</table>";
This code gives me a blank output.
i used following code and got output but some errors are still there.
$result = file_get_contents($base);
$obj = json_decode($result, true);
echo"<table border=1>";
$i = 0;
foreach($obj['response'] as $item) {
for($i=0;$i<=10;$i++)
{
echo"
<tr>
<td>{$item['data'][$i]['Offer']['name']}</td>
<td>{$item['data'][$i]['Stat']['payout']}</td>
<td>{$item['data'][$i]['Stat']['sale_amount']}</td>
<td>{$item['data'][$i]['Stat']['status']}</td>
<td>{$item['data'][$i]['Stat']['datetime']}</td>
<td>{$item['data'][$i]['Stat']['ip']}</td>
</tr>
";
}
}
echo"</table>";
my output:
why upper rows are displaying blank values when there is no blank data is present?
and what are those errors?
You're using the wrong syntax. It's difficult to see exactly the results you're expecting, the values of the objects being returned look like they are objects of the original json themselves and without any useful data. However, to clarify for you:
You're returning the json_decode() results as an object and trying to access it as an array.
To return an associative array you need to add the 'assoc' parameter to the function, to read:
json_decode($result, true)
and you were trying to eco out the value directly from the associative array, you need to access it via the key, which will give you the value to read:
$result = file_get_contents($base);
$obj = json_decode($result, true);
echo"<table>";
foreach($obj['request'] as $item) {
echo"
<tr>
<td>{$item['fields'][0]}</td>
<td>{$item['fields'][1]}</td>
<td>{$item['fields'][2]}</td>
<td>{$item['fields'][3]}</td>
<td>{$item['fields'][4]}</td>
<td>{$item['fields'][5]}</td>
</tr>
";
}
echo"</table>";
Also, I've added the curly brackets inside the string to escape the variable expressions.
Since you're getting objects in your response, first try json_decode($result->request) and generally don't use [ ] as you normally would as if your result was array
EDIT: Try outputting the response like this:
echo '<pre>';
print_r($your-variable-here);
echo '</pre>';
Then let me know of the output

trying to access json results in php

Im trying to display some data im receiving from the cloudstack API. I can display some of the data I want when I use the JSON as objects, with the following code;
stdClass Object
(
[listvirtualmachinesresponse] => stdClass Object
(
[count] => 2
[virtualmachine] => Array
(
[0] => stdClass Object
(
[id] => bab03c9b-94e0-429f-906c-8853de306e9a
[name] => LinuxL1
[displayname] => LinuxL1
[account] => admin
[domainid] => a3121682-837b-11e3-964c-001372f94016
[domain] => ROOT
[created] => 2014-05-08T13:43:01+0200
[state] => Running
[haenable] =>
[zoneid] => 7a3ee2f0-540a-4232-8264-50a47a91001d
[zonename] => Amsterdam
[hostid] => 024b9ca2-4076-488e-ad13-86dfb7f2b766
[hostname] => ptvirt6
[templateid] => cf6abb42-574f-4840-886a-8662c5914166
[templatename] => CentOS 6.5
[templatedisplaytext] => good
[passwordenabled] =>
[isoid] => cf6abb42-574f-4840-886a-8662c5914166
[isoname] => CentOS 6.5
[isodisplaytext] => good
[serviceofferingid] => 4fc30c59-4600-44ed-b0bb-7fc48e795d90
[serviceofferingname] => L1
[cpunumber] => 1
[cpuspeed] => 1000
[memory] => 1024
[cpuused] => 0.02%
[networkkbsread] => 55
[networkkbswrite] => 1
[diskkbsread] => 1000680
[diskkbswrite] => 1301501
[diskioread] => 77192
[diskiowrite] => 8884
[guestosid] => a92508f4-837b-11e3-964c-001372f94016
[rootdeviceid] => 0
[rootdevicetype] => ROOT
[securitygroup] => Array
(
[0] => stdClass Object
(
[id] => edfe848c-837b-11e3-964c-001372f94016
[name] => default
[description] => Default Security Group
[account] => admin
[ingressrule] => Array
(
)
[egressrule] => Array
(
)
[tags] => Array
(
)
)
)
[nic] => Array
(
[0] => stdClass Object
(
[id] => bfaeb76d-7b5a-46b1-aaa7-7dd15e86bf2e
[networkid] => 83007a42-e4b7-4ea2-aa21- 20938ea3bc56
[networkname] => defaultGuestNetwork
[netmask] => 111.111.111.111
[gateway] => 111.111.111.111
[ipaddress] => 111.111.111.111
[isolationuri] => ec2://untagged
[broadcasturi] => vlan://untagged
[traffictype] => Guest
[type] => Shared
[isdefault] => 1
[macaddress] => 06:a5:24:00:00:0a
)
)
[hypervisor] => KVM
[instancename] => i-2-9-VM
[tags] => Array
(
)
[affinitygroup] => Array
(
)
[displayvm] => 1
[isdynamicallyscalable] =>
)
I can read the name / state from the list with a foreach loop
<?php
$api_content = file_get_contents("http://127.0.0.1:5950/client/api?command=listVirtualMachines&domainid=1&account=admin&response=json");
$api_data = json_decode($api_content);
$ap_machine = $api_data->listvirtualmachinesresponse->virtualmachine;
echo "<pre>";
print_r($api_data);
echo "</pre>";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<table border="1" width="300">
<tr>
<td>Naam</td>
<td>Status</td>
<td>Ip Address</td>
<td>Health</td>
</tr>
<?php
echo "<tr>";
foreach($ap_machine as $item){
echo "<td>".$item->name."</td>";
echo "<td>".$item->state."</td>";
echo "<td>".$item->nic->ipaddress."</td>";
if($item->state == 'Running'){
echo '<td><img src="/600px-Approve_icon.svg.png" height="50" width="50"> </td>';
}
else{
echo '<td><img src="/fail.png" height="25" width="25"></td>';
}
echo "</tr>";
}
?>
I've read online to make an associative array from the json list, I did that aswell and didnt get a result.
In your PHP code you are accessing nice key as a normal element of json:
echo "<td>".$item->nic->ipaddress."</td>";
But, from your var_dump output I can see its of array type.
So, to access elements of nice array, you'l have to access it by specifying array index.
echo "<td>".$item->nic[0]->ipaddress."</td>";
Look dude
change
$api_data = json_decode($api_content);
to
$api_data = json_decode($api_content, TRUE);
this makes it an associative array
check http://www.php.net/manual/en/function.json-decode.php

php mongodb, sort multidimensional array

As I can make an ordered descending by the query field bytes each IP. This is my array contains other fields as refered or other browsers ....
[_id] => MongoId Object (
[$id] => 528e6004b0a4191f698b4567
)
[FECHA] =>.........
[IP] => Array (
[0] => Array (
[Ip] => 172.17.10.
[conexiones] => 71
[bytes] => 122.75 KB
[media] => 1770.338
)
[1] => Array (
[Ip] => 192.168.6.145
[conexiones] => 79
[bytes] => 692.51 KB
[media] => 8976.3164
)
[2] => Array (
[Ip] => 172.17.108.3
[conexiones] => 2
[bytes] => 4.48 KB
[media] => 2294.5
)
[3] => Array (
[Ip] => 192.168.200.50
[conexiones] => 123
[bytes] => 6.10 MB
[media] => 52004.699186992
...............
This is my code:
$Filtro="IP";
$c = $collection->find(array('FECHA' => $Fecha), array($Filtro));
$cont = 1;
while ($c->hasNext()) {
$metodo = $c->getNext();
foreach ($metodo[$Filtro] as $f) {
?>
<tr>
<td><?= $cont++ ?></td>
<td><?= $f[$Filtro] ?></td>
<td><?= $f['conexiones'] ?></td>
<td><?= $f['bytes'] ?></td>
<td><?= $f['media'] ?></td></tr>
<?
}
}
Just shows me all data but unsorted. thanks

Categories