I have following JSON Data and I tried to display Json data using PHP but it did not display result. Here is my JSON Data
$ads={"ReferenceNo":"KWPOG0QoXU","Message":"SUCCESS","Status":1,"ResponseStatus":200}
I tried following code but it did not work.
$ads1 = json_decode($ads);
echo $ads1->ReferenceNo;
Kindly help me how to display result in PHP. Thanks in advance.
You should parse data as string & then do like this:
$json = '{"ReferenceNo":"KWPOG0QoXU","Message":"SUCCESS","Status":1,"ResponseStatus":200}';
$array = json_decode($json, true);
echo '<pre>'; print_r($array);
Output:
Array
(
[ReferenceNo] => KWPOG0QoXU
[Message] => SUCCESS
[Status] => 1
[ResponseStatus] => 200
)
To get Data, Code like this:
echo $array['ReferenceNo'];
Let me know for further help.
For people interested in pulling data from a location, instead of adding the json on the file, use the following;
$json = file_get_contents('MYLOCATION.json');
$array = json_decode($json, true);
echo '<pre>'; print_r($array);
happy coding :)
Related
I'm trying to follow http://woocommerce.github.io/woocommerce-rest-api-docs/?php#list-all-order-notes to get order notes from my orders and get the author and id inside a nested json respons but it's not working
Does anything stand out as being wrong here?
require_once 'auth.php';
$notes = $woocommerce->get('orders/108668/notes');
$json = json_decode($notes, true);
foreach($json as $elem) {
echo($elem['id']. ", ".$elem['author'] );
echo("<br/>");
}
If i print_r($notes); I get the response like so
Array ( [0] => stdClass Object ( [id] => 24721 [author] => haspden [date_created] => 2020-12-07T09:03:01 [date_c........
if i print_r($json); i get nothing :/
Any thouhgts? I'm sure it something obvious and due to my missunderstanding between arrays and objects with json.
Thanks
Try casting $notes with array and expose it as json using json_encode:
$notes = $woocommerce->get('orders/108668/notes');
header('Content-Type: application/json'); // this line is to make sure the output format is JSON
echo json_encode((array)$notes);
As I understand from what you supplied, the response from $notes = $woocommerce->get('orders/108668/notes'); is already an array and you can loop through it and do whatever you like. You don't need to json_decode it.
Moreover, json_decode first parameter is string as the docs says but you supplied an array.
i have below json code
{"1":1,"5":1}
when i decode the above json i got object array using the below php statement.
$array_val = (array)json_decode($price);
i got a below array.
Array
(
[1] => 1
[5] => 1
)
but the below statement does not work
echo $array_val[1];
the below error occurred.
Undefined offset: 1
How to resolve this issue?
try this DEMO
PHP
$json = '{"1":1,"5":1}';
$array_val=json_decode($price, true);
echo $array_val[1];
OUTPUT :
1
Note that json_decode($string) returns an object, not an array (which is why your code doesn't behave).
To return an array instead, use:
$arr = json_decode($string, true);
See also http://php.net/manual/en/function.json-decode.php
You can get this using below code
$array_val=json_decode($price);
echo $array_val->{1}
OR
$array_val=json_decode($price,true);
echo $array_val[1]
I have a string on my databas, that I'm trying to insert into a json object as an Array, not a string..:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
$jsonObject = array('numbers' => $arrayInString);
$json = json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
echo $json;
When I execute this.. my Json object is..
numbers: "[2,3,5,5,6]";
and not
numbers: [2,3,5,5,6];
Like I originally wanted.. can't get this to work, can anyone help?
Like was said you need to decode the passed data from the database and then build your array output. Something like this:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
// decode the JSON from the database as we build the array that will be converted back to json
$jsonObject = array('numbers' => json_decode($arrayInString));
echo json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
The slightly modified code above outputs:
{"numbers":[2,3,5,5,6]}
You need to json_decode your $arrayInString variable before you add it to the associative array.
I been looking thru the posts here all day but can't figure out what I'm doing wrong. (I'm new to php and json)
Here is my code that work.
$json = '{"id":1234,"img":"1.jpg"}';
$data = json_decode($json, true);
echo $data["img"];
But when the json respond is this
$json = '{"demo1":[{"id":1234,"img":"1.jpg"}],"userId":1}';
it's a big harder for me. then img is a child of demo1? How to get it?
Thx. :)
Figuring out the array indices
As you're new to PHP, I'll explain how to figure out the array indces of the required array value. In PHP, there are many functions for debugging — print_r() and var_dump() are two of them. print_r() gives us a human-readable output of the supplied array, and var_dump() gives us a structured output along with the variable types and values.
In this case, print_r() should suffice:
$json = '{"demo1":[{"id":1234,"img":"1.jpg"}],"userId":1}';
$data = json_decode($json, true);
// wrap the output in <pre> tags to get a prettier output
echo '<pre>';
print_r($data);
echo '</pre>';
This will produce the following output:
Array
(
[demo1] => Array
(
[0] => Array
(
[id] => 1234
[img] => 1.jpg
)
)
[userId] => 1
)
From there, it should be pretty easy for you to figure out how to access the required vaule.
$data['demo1'][0]['img'];
Creating a helper function for ease of use
For ease of use, you can create a helper function to make this process easier. Whenever you want to view the contents of an array, you can simply call dump_array($array); and be done with it. No more messing around with <pre> tags or print_r().
Function code:
function dump_array($array) {
echo '<pre>' . print_r($array, TRUE) . '</pre>';
}
Usage example:
$arr = ['foo' => range('a','i'), 'bar' => range(1,9)];
dump_array($arr);
after decoding :
echo $data->demo[0]->img;
Basically, a { in JSON leads to a -> (it's an object).
And a [ to a [], it's an array.
JSON is like
{
"#http_status_code": 200,
"#records_count": 200,
"warnings": [],
"query": { ... ...
In PHP
$data = json_decode($json_entry);
print $data->#http_status_code; //returns error
print $data->http_status_code; //returns nothing
How can I retrieve status code?
1) As Object way
$data = json_decode($json_entry);
print $data->{'#http_status_code'};
2) OR use as array way by passing second argument as true in json_decode
$data = json_decode($json_entry, true);
print $data['#http_status_code'];
Try json_decode to get the json in form of array ..
$json_array = json_decode($data, true);
$required_data = $data['required_key']
with reference to your particular problem .. you will get array as
Array
(
[#http_status_code] => 200
[#records_count] => 200
[warnings] => Array
(
)
....
)
so you can access you data as $data['#http_status_code']
To access an object property that has funky characters in the name, quote the name and stick it in braces.
print $data->{'#http_status_code'};
Or, say $data = json_decode($json_entry, true); to get the data back as an array.
PHP cwill give syntax error when you do this:
$data->#http_status_code;
it looks for $http_status_code variable which is not present
so in order to make this work you have to do this:
echo $data->{'#http_status_code'};
Try this:
$data = json_decode($json_entry, true);
print $data["#http_status_code"];