I've never worked with json before and I'm unsure how to fully. So essentially I need to get all the items in the rgDescriptions table, but I am honestly unsure how to.
So if someone could point me in the right direction that would be great. I've tried to look for documentation/tutorials on Json but can't seem to find advanced stuff.
Only stuff like {"id":"2","instanceID":"8"}
Example of the json I want to decode
use json_desode() to decode the json format.
$json = '{"id":"2","instanceID":"8"}';
$decoded = json_decode($json);
print_r($decoded);// will print decoded format of your json
echo $decoded->id; // outputs => 2
echo $decoded->instanceID; // outputs => 8
Otuput:
stdClass Object
(
[id] => 2
[instanceID] => 8
)
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 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 :)
I not which part I am doing wrong. I couldn't able to fetch this array to display. Can someone please help me with this. I am new to JSON.
Array
(
[0] => [{"id":2,"request_id":2,"message":"wqvewq ewq wq ewq e wqwe qwe ","user_id":1,"created_at":"2014-05-30 16:21:28","updated_at":"2014-05-30 16:21:28"},{"id":3,"request_id":2,"message":"as aS A","user_id":2,"created_at":"2014-05-30 17:18:37","updated_at":"2014-05-30 17:18:37"},{"id":4,"request_id":2,"message":"AS As a","user_id":2,"created_at":"2014-05-30 17:18:43","updated_at":"2014-05-30 17:18:43"}]
[1] => [{"id":1,"request_id":2,"message":"sfsdfds sdfds f ","user_id":2,"created_at":"2014-05-30 17:15:16","updated_at":"2014-05-30 17:15:16"}]
[2] => []
)
The output you have quoted looks like PHP print_r output, and it's certainly not legal JSON.
Perhaps you need the PHP json_encode function, to get real JSON out of your PHP code?
It's not one json string but an array of json strings.
You have to first loop thru the array, parse the json and show the variables that you want in your html with jQuery.
You can find a lot of info on the internet and stackoverflow on this subject.
these are the possibilitys you have
var data = array();
for(var i=0;i<yourArray.length;i++)
data[i] = $.parseJSON(yourArray[i]);
or (untested)
var data = JSON.parse(JSON.stringify({yourArray: yourArray}));
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.
A JSON array has the form:
[[a,b,c],[a,b,c],[a,b,c]]
Is there a better way than split?
No, this is most certainly not the best way to parse JSON. JSON parsers exist for a reason. Use them.
In JavaScript, use JSON.parse:
var input = '[[1,2,3],[1,2,3],[1,2,3]]';
var arrayOfArrays = JSON.parse(input);
In PHP, use json_decode:
$input = '[[1,2,3],[1,2,3],[1,2,3]]';
$arrayOfArrays = json_decode($input);
You do not need to use regular expressions. As has been mentioned, you must first have valid JSON to parse. Then it is a matter of using the tools already available to you.
So, given the valid JSON string [[1,2],[3,4]], we can write the following PHP:
$json = "[[1,2],[3,4]]";
$ar = json_decode($json);
print_r($ar);
Which results in:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
)
If you want to decode it in JavaScript, you have a couple options. First, if your environment is new enough (e.g. this list), then you can use the native JSON.parse function. If not, then you should use a library like json2.js to parse the JSON.
Assuming JSON.parse is available to you:
var inputJSON = "[[1,2],[3,4]]",
parsedJSON = JSON.parse(inputJSON);
alert(parsedJSON[0][0]); // 1
In JavaScript , I think you can use Eval() → eval() method...