2

I create a views with REST export display, result are serialized with Json,

In my formBuild method I want to parse JSON and Convert it to array and use it in select option

    $host = \Drupal::request()->getHost();
    $response = \Drupal::httpClient()
        ->get($host.'/get/slider' );

    $json_string = $response->getBody()->getContents();
    kint($json_string);

I see Symfony serialize class and find Deserialize method with this example

 $person = $serializer->deserialize($data, 'Acme\Person', 'xml');

According to this,I guess maybe I should something like

     $results = $serializer->deserialize($json_string, '???', 'josn');

but what I should write in second param(???)? I try

    $serialize = new Serializer\Serializer();
    $json_string = $response->getBody()->getContents();
  $entity = $serialize->deserialize($json_string, '\Drupal\node\Entity\Node::class', 'json')  ;

but face me with error Symfony\Component\Serializer\Exception\UnexpectedValueException: Deserialization for the format json is not supported in Symfony\Component\Serializer\Serializer->deserialize().

And if you have better solution tell me How Can I deserilized views REST results?

update Also,I tried

    $json_string = $response->getBody()->getContents();
    $serializer = \Drupal::service('serializer');
    $deserialized = $serializer->deserialize($json_string, \Drupal\node\Entity\Node::class, 'json');

but Symfony\Component\Serializer\Exception\UnexpectedValueException: A string must be provided as a bundle value. in Drupal\serialization\Normalizer\EntityNormalizer->denormalize()

  • It might not be the only problem, but '\Drupal\node\Entity\Node::class' isn't right...that would either be '\Drupal\node\Entity\Node' or \Drupal\node\Entity\Node::class (no quotes). The latter being preferred – Clive Jun 1 '16 at 12:23
  • @Clive I tried but the same, I think I choose bad method to parse json to array in Drupal 8, in drupal 7 it was simple just json_decode . – Yusef Mohamadi Jun 1 '16 at 12:26
  • You can still use that (or Json::decode which is in D8), but that will leave you with an associative array like it would in D7. The idea of the serializer is to map the json straight back onto a classed object which is much better to work with – Clive Jun 1 '16 at 12:49
3

Drupal has a service for serialisation, pre-configured with the normalisers and encoders you need. The error you currently have is because you're not setting up the instance of the serialiser with the Symfony\Component\Serializer\Encoder\JsonEncoder, but you'll also need, for example, the Drupal\serialization\Normalizer\ConfigEntityNormalizer normaliser.

This code works well for me:

$serializer = \Drupal::service('serializer');
$node = \Drupal::entityTypeManager()->getStorage('node')->load(1);

$output = $serializer->serialize($node, 'json');
$deserialized_node = $serializer->deserialize($output, \Drupal\node\Entity\Node::class, 'json');

As always, inject the services if possible.

  • tnx dear Clive for attention, I try your solution , but Symfony\Component\Serializer\Exception\UnexpectedValueException: A string must be provided as a bundle value. in Drupal\serialization\Normalizer\EntityNormalizer->denormalize() , take a look on my question again( I update it with your suggest). how I use normalize ? – Yusef Mohamadi Jun 1 '16 at 12:52
  • What is you suggest Clive? Did you Think the @David Mcsmith solution is better? it work but I'm not sure it's good way. – Yusef Mohamadi Jun 4 '16 at 20:15
1

Try json_decode, first use Drupal\Core\Serializer;

then in your code use

 $json_string = $response->getBody()->getContents();  
 $res =json_decode($json_string);
  • your solution worked, but I am not sure it is the best way,? – Yusef Mohamadi Jun 4 '16 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.