This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 4 months ago.
I can show the FactorDate item with the following code.
$response=json_decode($response->getBody());
foreach($response as $product)
{
echo $product->FactorDate;
}
but I can not show the Price item. please guide me.
[
{
"CustomerCode": 101,
"FactorNumber": 53,
"FactorDate": 14010201,
"FactorDetails": [
{
"ProductCode": 21901,
"Count": 15,
"Price": 96000000,
"VisitorID": 0
}
]
}
]
The JSON that you had provided looks like price is a part of an array within FactorDetails. Meaning that if you want to get the price you will likely have to do something like:
$product->FactorDetails[0]->Price
Or put into your example, something like this will display both Date and Price:
$response=json_decode($response->getBody());
foreach($response as $product)
{
echo $product->FactorDate;
echo $product->FactorDetails[0]->Price;
}
That should work.
For future reference, you could use a tool like this JSON viewer to see a more readable version of your JSON which can be super helpful when dealing with formatting issues like this one:
https://codebeautify.org/jsonviewer
Related
This question already has answers here:
PHP. Is it possible to use array_column with an array of objects
(5 answers)
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 1 year ago.
Hi I'm currently trying to do an API request. The API sends out a json request like this:
[
{
"name": "Test 1"
"yes-or-no": "yes"
},
{
"name": "Test 2"
"yes-or-no": "no"
}
]
My question is, how do I select one of the yes-or-no to echo in the website? I tried doing this:
<?php
$status = json_decode(file_get_contents('url to the json file'));
// Display message AKA uptime.
foreach ($status->yes-or-no as $answer) {
echo $answer.'<br />';
}
?>
But didn't work.
I'm sorry if I got some terms wrong, since I'm pretty new to coding APIs like this.
EDIT: Please see the answer below. It works but now my question is: How do I only display one of them? Instead of both of them displaying at the same time.
I'm not really sure what you are trying to do, but maybe i can shed some light into the question:
$status = json_decode(file_get_contents('url to the json file'), true);
Add ", true" this will make your $status an array instead of an object.
foreach ($status as $answer) {
echo $answer['yes-or-no'].'<br />'; //output yes or no
echo $answer['name'].'<br />'; //output test 1 or test 2
}
Try something like this:
<?php
$statuses = json_decode(file_get_contents('url to the json file'));
foreach ($statuses as $status) {
echo $status->{'yes-or-no'};
}
?>
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
The API is returning the following json:
{
"car1": [
{
"car_name": "audi",
"gear_box_type": "Automatic",
"num_of_seats": "2",
"num_of_doors": "3",
"imagePath": "/images/a3.png"
}
]
}
The following is my php code:
<select class="form-control" id="exampleFormControlSelect1">
<?php
$obj = new ApiHelper();
$result = $obj->CallAPI("GET","http://localhost:5000/cars", "NONE");
$jsondata = json_decode($result, true);
$car = $jsondata['car1'];
echo "<option>".$car."</option>";
?>
</select>
The output will be "Array".
Image of select
I would like to know how I can access this array that is inside the json.
Any help much appreciated thanks.
The array is accessed like any standard PHP array. You can Google search "PHP arrays" to find more information. One helpful function is print_r($array_name).
So:
print_r($jsondata);
Will give you a printout of the structure of the array, making it easy for you to then determine what is available to you in the array. If you view the output of this function in your browser, be sure to right-click on the page and select "View Source." Otherwise the structure will be printed out as one long string that isn't very fun to read.
Hope this helps!
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I cannot access the contents within the orders array. I am currently doing this to no avail, and am wondering what I am doing wrong:
the $json object is a json response from a rest api.
$orderData = $json['orders'];
foreach($orderData['orders'] as $order){
}
{
"errors":[
],
"orders":[
{
"note":"",
"estimated_shipping_fee":"0.00",
"payment_method":"PAY_CYBERSOURCE",
"escrow_amount":"12.95",
"message_to_seller":"",
"shipping_carrier":"Singpost - Normal Mail",
"currency":"SGD",
"create_time":1532064559,
"pay_time":1532064618,
"recipient_address":{
"town":"",
"city":"",
"name":"Teo",
"district":"",
"country":"SG",
"zipcode":"41253",
"full_address":"In some street somewhjere",
"phone":"23154991",
"state":""
},
"days_to_ship":3,
"tracking_no":"",
"order_status":"SHIPPED",
"note_update_time":0,
"update_time":1532082525,
"goods_to_declare":false,
"total_amount":"12.95",
"service_code":"",
"country":"SG",
"actual_shipping_cost":"",
"cod":false,
"items":[
{
"weight":1.0,
"item_name":"ABC",
"is_wholesale":false,
"item_sku":"5123433123",
"variation_discounted_price":"12.95",
"variation_id":0,
"variation_name":"",
"item_id":1126534500,
"variation_quantity_purchased":1,
"variation_sku":"",
"variation_original_price":"12.95"
}
],
"ordersn":"3589290984539"
}
]
}
Trying to access the variables directly, by say json['orders'][0]['payment_method'] is not working.
convert it to array from json using json_decode(); then you can easily access it and if you want to access using jquery then just convert it object using jQuery.parseJSON();
just use json_decode for retrieving the object and then $obj->note or you can turn it into an array: $array = get_object_vars($obj); like in this answer .
This question already has an answer here:
Dealing with special characters in object property names
(1 answer)
Closed 6 years ago.
Here is my sample JSON:
[
{
"#attributes":{
"name":"Stack Overflow Movies ",
"root":"http:\/\/www1.stackovermovies.com\/",
"id":"1",
"address":"Liverpool",
"postcode":"PZ203434"
},
"films":{
"film":[
{
"#attributes":{
"synopsis":"Description goes here.\r\n",
"poster":"http:\/\/www1.stackoverflowmovies.com\/posters\/HO00003702.jpg",
"cast":"Wayne Max",
"director":"",
"length":"125 mins",
"title":"X-Men (2016)",
"rating":"12A",
"release":"19\/11\/2016"
},
"shows":{
"show":{
"#attributes":{
"date":"Sat 19 Nov",
"time":"17:00"
}
}
}
},
I tried to decode it:
The variable $feed contains the above JSON.
I tried to get the name attribute
echo $feed->'#attributes[0]'->name; but this is not working because of the # character. Is there a workaround? I tried looking into CDATA but can't figure out if it's the right solution.
Use json_decode($json, true) to get an array instead of an object. Then you can access your field like this:
$feed[0]['#attributes']['name'];
One way to access it is to provide a string value for the key in the object, like this:
$decoded->{'#attributes'};
This question already has answers here:
Pretty-Printing JSON with PHP
(27 answers)
Closed 8 months ago.
$arr = array(
'toemail'=>$v->agent_primary_email,
'agentname'=>$v->agent_firstname,
'agentid'=>$v->agent_id,
'subject'=>'The details of total number of properties saved by your clients',
'totalprop'=>$v->prop_count
);
echo json_encode($arr);exit;
The output looks like this
{"toemail":"abc#gmail.com","agentname":"john","agentid":"110012","subject":"The details of total number of properties saved by your clients","totalprop":"131"}
But what changes should i have make, so that the output looks like this
{"toemail":"abc#gmail.com",
"agentname":"john",
"agentid":"110012",
"subject":"The details of total number of properties saved by your clients",
"totalprop":"131"}
Use JSON_PRETTY_PRINT and also need to use echo "<pre>";
From PHP Manual: Use whitespace in returned data to format it. Available since PHP 5.4.0
$array = array(
'test'=>1,
'test2'=>'test',
'test3'=>'test 3'
);
echo "<pre>";
echo json_encode($array,JSON_PRETTY_PRINT);
Result:
{
"test": 1,
"test2": "test",
"test3": "test 3"
}