JSON http://drupal8.ovh/en/tags/json en JSON Encode and Decode http://drupal8.ovh/en/tutoriels/145/json-encode-and-decode <span class="field field--name-title field--type-string field--label-hidden">JSON Encode and Decode</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to encode and decode JSON array/string in drupal 8 way like drupal_json_encode and drupal_json_decode in drupal 7 ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Wed, 05/18/2016 - 23:02</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Example :</p> <p><code>use Drupal\Component\Serialization\Json;<br /> $json = Json::encode($data);<br /> $data = Json::decode($json);</code></p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/json" hreflang="en">JSON</a></li> </ul> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=145&amp;2=comment&amp;3=comment" token="7fef831d"></drupal-render-placeholder> </section> Wed, 18 May 2016 21:02:49 +0000 editor 145 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/145/json-encode-and-decode#comments Return JSON array as resut - Provide Json Interface http://drupal8.ovh/en/tutoriels/32/return-json-array-as-resut-provide-json-interface <span class="field field--name-title field--type-string field--label-hidden">Return JSON array as resut - Provide Json Interface</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to provide json array as result of service or as output page.</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Fri, 11/20/2015 - 09:47</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>You can do that using Symfony JsonResponse.</p> <p>Exemple 1.<br /><code>return new \Symfony\Component\HttpFoundation\JsonResponse($array);</code></p> <p>Exemple 2.<br /><code>use Symfony\Component\HttpFoundation\JsonResponse;<br />   public function get_result() {<br />     $output = array();<br />     $output[] = array('time' =&gt; date("Y-m-d H:i:s"));<br />     $output[] = array('result'=&gt; "OK");<br />     return new JsonResponse($output);<br />   }</code></p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/json" hreflang="en">JSON</a></li> </ul> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=32&amp;2=comment&amp;3=comment" token="b2af8325"></drupal-render-placeholder> </section> Fri, 20 Nov 2015 08:47:20 +0000 editor 32 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/32/return-json-array-as-resut-provide-json-interface#comments