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'};
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 10 months ago.
This post was edited and submitted for review 10 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Please see json below
{
"id": "1",
"name": "john",
"data": {
"1": {
"amount": "9000",
"slip": "/image'/loidsds.jpg"
},
"method": "pump"
}
}
I am trying to read amount and slip how can i manage to do this is PHP ?
I have tried the following
$object->data->1->amount and $object->data->1->slip
I got some errors please help me find an alternative
Decode as an array by passing a truthy value as the second argument to json_decode():
$json = json_decode($string, true);
$amount = $json['data'][1]['amount'];
$slip = $json['data'][1]['slip'];
Or decode as an object, but then you have to brace the 1 because it's not normally a valid attribute name:
$json = json_decode($string);
$amount = $json->data->{1}->amount;
$slip = $json->data->{1}->slip;
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 answers here:
How can I access a deep object property named as a variable (dot notation) in php?
(5 answers)
Closed 4 years ago.
I have a .json configuration file. Using PHP, I'm trying to get its contents as an object by json_decode() and validate it. The JSON file contains multi-dimensional data. The problem is with accessing a member in depth dynamically.
For instance, consider the following data:
{
"somebody": {
"name": "Ali",
"age": 13,
"life": {
"stat": "good",
"happy": true
}
}
How to access the value of the following dynamically?
$happy = $data->somebody->life->happy;
What I mean from a dynamic access is something like this:
$happyIndex = "somebody->life->happy";
$happy = $data->{$happyIndex};
Also, I don't want to use eval().
Thanks.
Assuming you have a JSON data return
data="{
"somebody": {
"name": "Ali",
"age": 13,
"life": {
"stat": "good",
"happy": true
}
}"
data = jQuery.parseJSON(data);
then you can navigate to different parts of data by navigating
var name= data.somebody[0].name;
var age= data.somebody[0].age;
You will need a $.each function if you have more than one data in "somebody" array.
PHP Version:
$data='{"somebody":{"name": "Ali","age": "13","life": {"stat": "good","happy": "true"} }}';
$data = json_decode($data,TRUE);
$name= $data['somebody']['name'];
$age= $data['somebody']['age'];
echo("<pre>");
echo($name);
echo($age);
echo("</pre>");
//OUTPUT RESULT Ali 13
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
How do I get the uname value from this JSON into PHP variable? to use through my page?
{
"token": "iutiutiut-0jjjjj0-97987g",
"auth": {
"id": 1,
"app_id": 1,
"user": {
"uname": "foo",
"role": "member"
}
}
}
thanks for some reason just can't get it and I can't find examples similar anywhere on google or I am not calling it correctly.
could you also tell me the correct terminology so I know as well thanks
What you're trying to do is decode JSON. PHP has a built in function for this aptly named... json_decode. Assuming your JSON is a string ($json_string), here is how you would decode it:
$obj = json_decode($json_string);
$uname = $obj->auth->user->uname;
Or, if you prefer the array syntax, use the second argument in json_decode:
$arr = json_decode($json_string, true);
$uname = $obj['auth']['user']['uname'];