This question already has an answer here:
how to decode this JSON string?
(1 answer)
Closed 8 years ago.
Respected All,
I have a string which I got from an api. Now I want to extract useful data from this string. This output is a result of Json query.
the string is
{"PR":{"Url":"http://www.ididthisfilm.com/lex_tmp2/custom-form/","Domain":"http://www.ididthisfilm.com","Title":"Lexicon Of Sustainability – Custom Form","Description":"Welcome to THE LIST EDIT FORM","Pictures":[{"Url":"http://www.ididthisfilm.com/lex_tmp2/wp-content/uploads/2014/05/Lexicon_logo_new_BETA.png","Alt":"Lexicon Of Sustainability","Title":"","SourceType":1,"Width":0,"Height":0,"ParsedAspectRatio":0}],"Videos":[],"AuthorName":null,"ExtraInfo":null},"EM":null}`
How can I get title and description and url in different variables so that I can use them in php.
use built-in function json_decode and you will get the JSON object.
$str=<<<CODE
{"PR":{"Url":"http://www.ididthisfilm.com/lex_tmp2/custom-form/","Domain":"http://www.ididthisfilm.com","Title":"Lexicon Of Sustainability - Custom Form","Description":"Welcome to THE LIST EDIT FORM","Pictures":[{"Url":"http://www.ididthisfilm.com/lex_tmp2/wp-content/uploads/2014/05/Lexicon_logo_new_BETA.png","Alt":"Lexicon Of Sustainability","Title":"","SourceType":1,"Width":0,"Height":0,"ParsedAspectRatio":0}],"Videos":[],"AuthorName":null,"ExtraInfo":null},"EM":null}
CODE;
print_r( json_decode($str) );
which outputs
stdClass Object
(
[PR] => stdClass Object
(
[Url] => http://www.ididthisfilm.com/lex_tmp2/custom-form/
[Domain] => http://www.ididthisfilm.com
[Title] => Lexicon Of Sustainability – Custom Form
[Description] => Welcome to THE LIST EDIT FORM
[Pictures] => Array
(
[0] => stdClass Object
(
[Url] => http://www.ididthisfilm.com/lex_tmp2/wp-content/uploads/2014/05/Lexicon_logo_new_BETA.png
[Alt] => Lexicon Of Sustainability
[Title] =>
[SourceType] => 1
[Width] => 0
[Height] => 0
[ParsedAspectRatio] => 0
)
)
[Videos] => Array
(
)
[AuthorName] =>
[ExtraInfo] =>
)
[EM] =>
)
if any error occurs, try this: json_last_error()
Related
This question already has answers here:
Pretty-Printing JSON with PHP
(27 answers)
Closed 6 months ago.
Having an array printed with print_r method:
Array
(
[0] => Array
(
[id] => 44
[item_level] => 0
[position] => 10
[parent_position] =>
[title] => PHP Tutorial
[qty] => 0
[comment] =>
[status] =>
[unit] =>
)
[1] => Array
(
[id] => 46
[item_level] => 1
[position] => 20
[parent_position] =>
[title] => Algorithms
[qty] => 1
[comment] =>
[status] =>
[unit] =>
)
[2] => Array
(
[id] => 48
[item_level] => 2
[position] => 30
[parent_position] => 20
[title] => PHP and MySQL Databases
[qty] => 1
[comment] =>
[status] =>
[unit] =>
)
)
I'm trying to encode it as JSON using either:
json_encode($array, JSON_NUMERIC_CHECK);
json_encode($array);
The output in both cases is the following though:
[{"id":"44","item_level":0,"position":10,"parent_position":"","product_id":"","title":"PHP
Tutorial","qty":0,"comment":"","status":"","unit":""},{"id":"46","item_level":1,"position":20,"parent_position":"","title":"Algorithms","qty":1,"comment":"","status":"","unit":""},{"id":"48","item_level":2,"position":30,"parent_position":20,"title":"PHP
and MySQL
Databases","qty":1,"comment":"","status":"","unit":""}]
Yes, there are the newline characters put between some, but not all, space-separated words. In Postman, the response arrives broken into multiple lines, the same happens when my frontend gets this kind of response, causing it to report JSON parsing error.
How to transform the array to JSON format properly?
PHP version here is 7.0.33.
Tried to look for any non-printable characters that might have been added on the way, as it was suggested in the comments, but wasn't able to locate any.
Anyway, pretty printing solved the issue in a satisfying way:
json_encode($array, JSON_PRETTY_PRINT);
The only thing to be considered, is casting numbers to proper types on arrival to frontend, since JSON_PRETTY_PRINT prints out all of them as strings (i.e. {"quantity": "1"}).
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
I have a JSON data which I am trying to parse, but my code is not working. I get the error
unexpected '->' (T_OBJECT_OPERATOR).
The array structure looks like this:
Array
(
[mautic.lead_post_save_update] => Array
(
[0] => Array
(
[lead] => Array
(
[isPublished] => 1
[dateAdded] => 2016-09-15T08:08:20+00:00
[createdBy] => 1
[createdByUser] => Deepak Tiwari
[dateModified] => 2016-12-20T14:20:36+00:00
[modifiedBy] => 1
[modifiedByUser] => Deepak Tiwari
[id] => 149269
[points] => 100
[color] =>
[fields] => Array
(
[core] => Array
(
[compulsation] => Array
(
[id] => 39
[label] => Job
[alias] => compulsation
[type] => text
[group] => core
[field_order] => 2
[object] => lead
[value] => 0
and so on. I need to access the value of "job compultion". The code I am using is:
$json = file_get_contents('php://input');
$data=json_decode($json,TRUE);
$job=data->mautic.lead_post_save_update->0->lead->fields->core->compulsation->value;
I am not getting any value in $job.
You're using object property syntax (->) to access array elements, which won't work. Your code should use array syntax ([...]), like this:
$json = file_get_contents('php://input');
$data=json_decode($json,TRUE);
$job=data['mautic.lead_post_save_update'][0]['lead']['fields']['core']['compulsation']['value'];
That's what the error message is trying to tell you. A decent IDE, like PHPStorm (no affiliation), would help you catch errors like this.
Your json_decode() is not creating a object. Its an array and you can get your variables by using [] instead of the ->
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
i create an array and i would like to access to the value but i really don't know how :
Here is my array :
Array
(
[0] => Array
(
[id] => 1
[id_orga] => 36094152
[nom] => Adresse externe
[url] => https://cubber.zendesk.com/api/v2/organizations/36094152.json
[created] => 2015-01-22 08:00:53
[updated] => 2015-01-22 08:00:53
[tickets] => Array
(
)
[monthly] => Array
(
[0] => ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[assist] => 0
[maint] => 0
[total] => 0
)
)
and i would like for example to access to the value "0" in the key "assist" and change it to "1" for example and i don't know how to do it.
Suppose the source array is called $myArray. Try:
$myArray[0]['monthly'][0]->storage['assist'] = 1
If that doesn't work, try:
$myArray[0]['monthly'][0]['storage']['assist'] = 1
Let me know which works so I delete the other.
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
Find ID value through php
stdClass Object ( [Providers] => Array (
[0] => stdClass Object (
[Main_Fax] =>
[Signed_POA] => No
[Signed_HIPAA] => No
[Website] => www.google.com
[VAR_Provider_Type] => Template Test
[State_Temp] =>
[City] =>
[Notes] =>
[Main_Phone] =>
[ZIP] =>
[Provider_Type_Temp] =>
[State] => Alaska
[Signed_Purchae_Agreement] => No
[Reimbursement_Rate] => 0
[ID] => 977948000009029003
[Provider_Name] => Amit
[Address_Line_1] =>
[In_Network] => No
[Address_Line_2] =>
)
)
)
Seprate this json & show id value through php -> means this value [ID] => 977948000009029003
Let your array is $arr.
Now try this:
$arr->Providers[0]->ID;
Result:
977948000009029003
$data->Providers[0]->ID
See the structure of the object you gave us. You can see there's an object with a item called Providers. You can then see that the value of "Providers" is an array indexed by a integer, which inside it has an Object. The object contains the ID you wish to obtain.
Object > Array > Object
I am attempting to get around having to raise the max_input_vars parameter when submitting a large form. My thought was to jsonify the form, create a second form with a single field in it, put the jsonified string into that single field, submit, decode as an array, and then replace the POST array with the decoded data. I am plugging this into an existing system so for this to work I would need the new array to have the same structure as the raw post would. However, I am running into an issue where regardless of how I encode the data I am losing info and structure beyond the first depth of the array. I have tried both using this library to encode the form:
https://github.com/macek/jquery-serialize-object
As well as jQuery(form).serializeArray(), each of which delivers results that decode in their own unintended ways. This is what the raw post data looks like when I don't intercept the form and dump it with print_r($_POST):
Array
(
[closedpostboxesnonce] => 9d9dc8fa74
[meta-box-order-nonce] => 520ef5d263
[update-nav-menu-nonce] => cfea78920d
[_wp_http_referer] => /wp-admin/nav-menus.php
[action] => update
[menu] => 2
[menu-name] => Left Sidebar Menu
[save_menu] => Save Menu
[qtranslate-fields] => Array
(
[menu-item-title] => Array
(
[3871] => Array
(
[en] => Knowledge Center
[ja] => Knowledge Center
[ko] => Knowledge Center
[zh] => Knowledge Center
[qtranslate-separator] => [
)
The data when I use the jquery-serialize-object library above to encode the form data, then decode it with json_decode($_POST["allinputs"],true):
Array
(
[closedpostboxesnonce] => 9d9dc8fa74
[meta-box-order-nonce] => 520ef5d263
[update-nav-menu-nonce] => cfea78920d
[_wp_http_referer] => /wp-admin/nav-menus.php
[action] => update
[menu] => 2
[menu-name] => Left Sidebar Menu
[qtranslate-fields[menu-item-title][3871][en]] => Knowledge Center
[qtranslate-fields[menu-item-title][3871][ja]] => Knowledge Center
[qtranslate-fields[menu-item-title][3871][ko]] => Knowledge Center
[qtranslate-fields[menu-item-title][3871][zh]] => Knowledge Center
Using jQuery(form).serializeArray() to encode, and $allinputs = (array)(json_decode($_POST["allinputs"])); to decode:
Array
(
[0] => stdClass Object
(
[name] => closedpostboxesnonce
[value] => 9d9dc8fa74
)
[1] => stdClass Object
(
[name] => meta-box-order-nonce
[value] => 520ef5d263
)
[2] => stdClass Object
(
[name] => update-nav-menu-nonce
[value] => cfea78920d
)
Is there a way to get the data encoded and then decoded while retaining the original structure?
Edit: using #think-win-win 's suggestion of passing true as the second parameter of json_decode yields:
Array
(
[0] => Array
(
[name] => closedpostboxesnonce
[value] => 9d9dc8fa74
)
[1] => Array
(
[name] => meta-box-order-nonce
[value] => 520ef5d263
)
[2] => Array
(
[name] => update-nav-menu-nonce
[value] => cfea78920d
)
It is one of the ones that I tried, along with a couple of others, but I felt the post was getting too long to include them all. The above one loses the associativeness of the top level of the array, in addition to converting the deeper levels to:
[7] => Array
(
[name] => qtranslate-fields[menu-item-title][3871][en]
[value] => Knowledge Center
)
[8] => Array
(
[name] => qtranslate-fields[menu-item-title][3871][ja]
[value] => Knowledge Center
)