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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据