dsjk3214 2016-01-15 12:51
浏览 65
已采纳

使用正则表达式将字符串转换为JSON [关闭]

I have a string like this :

{
"XXX"       "XXX"
"XXX"       "XXX"
"XXX"
{
    "XXX"       "XXX"
    "XXX"       "XXX"
    "XXX"
    {
        "XXX"       "XXX"
    }
    "XXX"
    {
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
    }
    "XXX"       "XXX"
}
"XXX"
{
    "XXX"       "XXX"
    "XXX"
    {
        "XXX"       "XXX"
    }
    "XXX"
    {
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
    }
    "XXX"       "XXX"
}
"XXX"
{
    "XXX"       "XXX"
    "XXX"
    {
        "XXX"       "XXX"
    }
    "XXX"
    {
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
    }
    "XXX"       "XXX"
}
"XXX"
{
    "XXX"       "XXX"
    "XXX"
    {
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
        "XXX"       "XXX"
    }
    "XXX"       "XXX"
}
"XXX"
{
    "XXX"
    {
        "XXX"       "XXX"
    }
    "XXX"
    {
        "XXX"       "XXX"
        "XXX"       ""
    }
    "XXX"
    {
        "XXX"       "XXX"
        "XXX"       ""
    }
    "XXX"
    {
        "XXX"       "XXX"
        "XXX"       ""
    }
    "XXX"
    {
        "XXX"       "XXX"
        "XXX"       ""
    }
    "XXX"
    {
        "XXX"       "XXX"
        "XXX"       ""
    }
    "XXX"
    {
        "XXX"       "XXX"
        "XXX"       ""
    }
  }
}

It looks like JSON, but it isn't : it's lacking all the commas (',') and all the colons (":"). I'm trying to find a way to parse it and turn into a valid JSON string.

My guess is to use the tabulations and the line breaks to find my way in the string, but so far I wasn't able to convert it to valid JSON.

I came up with this, which works but I believe is not very efficient or fail safe :

$e = explode( "
", $string );
foreach( $e as $k => $l ) {

$next = str_replace( "\t", '', $e[ $k + 1 ] );
$isParam = strstr( $l, "\"\t\t\"" );
$currentClean = str_replace( "\t", '', $l );

if ( $isParam )
    $e[ $k ] = str_replace( "\"\t\t\"", '":"', $l );

if ( $isParam && $next != '}' )
    $e[ $k ] .= ',';

if ( $currentClean == '}' && $next != '}' && $next )
    $e[ $k ] .= ',';

if ( preg_match( '#^"(.*)"$#', $currentClean ) && !$isParam )
    $e[ $k ] .= ':';

}
$json = implode("
", $e);

How to improve this ?

  • 写回答

2条回答 默认 最新

  • dongzhimin2231 2016-01-15 14:42
    关注

    The problem is that you have several replaces at hand...so my answer comes with several calls (i try with all the text, it doesn´t need to be separated each line, just enter the full text).

    //For the ":" before "}" and keys
    $alltext = preg_replace("/(\"\w+\")\s+(\{|(\"\w*\"))/", "$1:$2", $alltext);
    
    //For "," at the ends of the values:keys
    $alltext = preg_replace("/(\"\w*\":\"\w*\")(?!\s+(\}|\{))/", "$1$2,", $alltext);
    
    //For the "," at the end of each "}" that has a value after
    $alltext = preg_replace("/(\})(\s+\")/", "$1,$2", $alltext);
    

    PD: Dont know what your values are so thats why I left the \w+ in the answer. Let me know if it was helpful.

    PD2:You can try it at phpliveregex

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题