dongyuan9109
2017-04-21 08:51
浏览 60
已采纳

JSON在PHP中解码多个嵌套的JSON

I am using a page builder called King composer for wordpress, where i am trying to build some custom functions, like is intended.

My problem is, that the build-in background color picker is base64 encoding the background properties, So i need to decode it - But first i need to decode the 'my-css' json, so that i can access the different properties.

this is the return of what i get from the builder.

array (
    '_id' => '69391',
    'image' => '294,9,16',
    'gallery-text' => 'Dette er nærmest et galleri',
    'my-css' => '{
           `kc-css`:{
                  `any`:{
                       `typography`{`color|`:`#ffffff`},
                       `background`{`background|`:`longBase64StringHere`},
                       `box`:{`margin|`:`100px inherit inherit inherit`}
                       }
                    }
                 }',
      )

So far i have tried:

 $decodedBackground = base64_decode($atts['my-css']);

which returns as null

then i tried :

 $decodedJson = json_decode($atts['my-css']);

which returns : null

Also tried some other stuff that went horriably wrong

I don't really understand it, I can access the other properties fine, since it is just a part of an array, but the CSS part, I cannot comprehend. I think I need to go deeper in - but I can't get it to work.

Been stuck for about 1.5 hours now, so any help or pointers would be appreciated

/------ EDIT -----/

So this is how i am trying to inspect the decoded json afterwards - might be important.

 $decodedJson = json_decode($atts['my-css'], true);
 echo '<pre>' . var_export($decodedJson, true) . '</pre>';
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • donglun1020 2017-04-21 09:04
    最佳回答

    This is maybe not the best way to do because the JSON in kc-css is not well formated, but this code works for your case:

    // Refomating JSON
    $atts['my-css'] = str_replace('`{', '`:{', $atts['my-css']);
    $atts['my-css'] = str_replace('`', '"', $atts['my-css']);
    
    $json = json_decode($atts['my-css'], true);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题