I have the following method to convert JSON to a String

// Convert the JSON-encoded image info into an array
$image = new JRegistry;
$image->loadString($this->item->image, 'JSON');
$this->item->imageDetails = $image;

But this code works only on an object. Is there any way to use these functions on an array?

  • From a shallow dig into the source code, it looks to me that the conversion only goes from object to string and string to object. Did you want to json_decode($this->item->image, true), then use ->loadArray()? – mickmackusa Mar 27 '20 at 21:32
  • Oh sure, thank you @mickmackusa – Thiago Guimarães Mar 28 '20 at 13:23
  • Is this what you are seeking to do? It works as you require? Shall I post it as an answer? Do you actually need it to be loaded into the registry? – mickmackusa Mar 28 '20 at 20:36
  • Actually i'm retrieving from the db and i wanted to follow along with the function above using the same function - but for arrays - to retrieve the image and its atributes; the probem is that i'm using array instead of object so i was trying to figure out how to obtain a similar result; therefore, your solution works perfectly; thank you once again. Before asking the question, though, i managed to implement a solution using json_decode in the default.php but the original function is placed in the model and i haven't used loadArray() but it's working all the same. Does it make any difference? – Thiago Guimarães Mar 30 '20 at 10:56
  • Perhaps I'll leave this question to be answered by some of the regulars here who have a far more intimate understanding of best practices within Joomla. – mickmackusa Mar 30 '20 at 11:46

Your Answer

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

Browse other questions tagged or ask your own question.