Does wordpress butcher my JSON strings?

[ad_1]

Hi everyone, I have a json string that is input by the user on an form in the edit post tab. I’m having trouble parsing it correctly after getting the string from the database:

$rawdata = get_post_meta( $current_id, ‘lele’, true );
$newdata = json_decode( $rawdata );
foreach ($newdata as $mydata) {…
Warning: foreach() argument must be of type array|object, string given in /var/www/atuff on line 29

I tried both to use stripslashes before and after decoding but with no use, is wordpress butchering my JSON strings? How can I retrieve data without any external tampering?

Many thanks!

[ad_2]
5 Comments
  1. If you var_dump( get_post_meta( $current_id, ‘lele’, true ) ); what does the value look like? Is it even a json string?

  2. No it’s not butchering your json. It’s expecting an array, and not finding it due to the json itself or something in your function. Post the json and the full function.

  3. first check if $rawdata contains what you expect it to.
    then check if $newdata contains what you think it should

  4. Look at the actual data in the database. If it’s not json, then json decode won’t work.

    Also, you do not have to store data encoded as json. WordPress can also store PHP arrays just fine. Built-in.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer