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 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备