doubaben7394 2018-02-14 06:11
浏览 135
已采纳

如何从数据库中读取json

I have json string in database

{"api":[{"caseid":2,"fullname":"df","businessname":"asdf","phonenumber":"12345678","activity":"sdf","province":7,"wilayat":"adfasd","description":"sfasdf","casedate":"2018-02-08 11:39:19"},{"caseid":3,"fullname":"\u0646\u0648\u0631\u062e\u0627\u0646 \u062a\u0627\u062a\u0627\u0631\u064a","businessname":"\u0627\u0644\u0631\u0641\u062f","phonenumber":"98818663","activity":"\u0646\u0649\u0633 \u064a\u0634\u064a \u0647\u062e\u0634 \u0645\u062a \u0634\u064a\u0628\u0634\u0633","province":1,"wilayat":"\u0627\u0644\u0633\u064a\u0628","description":"\u0648\u0635\u0641 \u0627\u0644\u0634\u0643\u0648\u064a","casedate":"2018-02-08 12:14:24"},{"caseid":4,"fullname":"test","businessname":"asdf","phonenumber":"12345678","activity":"asdfas","province":3,"wilayat":"dfasdf","description":"fsdfasdfasdf","casedate":"2018-02-08 12:24:53"},{"caseid":5,"fullname":"\u0645\u062d\u0645\u062f \u0627\u0633\u0644\u0627\u0645","businessname":"\u0635\u0646\u062f\u0648\u0642 \u0627\u0644\u0631\u0641\u062f","phonenumber":"98818663","activity":"\u0649\u0646 \u0633\u0634 \u0646\u062e \u0645\u0643 \u0648\u0646\u062e \u0635\u062e\u0636 \u062d\u062e\u0634","province":1,"wilayat":"\u0627\u0644\u0633\u064a\u0628","description":"\u0634\u0628 \u0634\u0633\u064a\u0628 \u0634\u0633\u064a\u0628 \u0634\u0633\u0628 \u062b\u0636\u0635  \u0631\u062b\u0631\u062b \u0631\u062b\u0631\u0631 \u0629\u0645 \u0629\u0648\u0648 \u0646\u0646\u062e\u062d","casedate":"2018-02-12 11:23:38"},{"caseid":6,"fullname":"\u0641\u0647\u062f \u0627\u0644\u062d\u0627\u0631\u062b\u064a","businessname":"\u062a\u062c\u0631\u0628\u0629 \u0627\u0644\u0646\u0638\u0627\u0645 ","phonenumber":"95871817","activity":"\u0644\u0627 \u064a\u0648\u062c\u062f ","province":1,"wilayat":"\u0627\u0644\u0633\u064a\u0628 ","description":"\u062a\u062c\u0631\u0628\u0629 1 ","casedate":"2018-02-13 13:40:51"}]}

$apiContent have above json string

$apiContent = $this->db->from("users_api")->get()->row()->apicontent;

when i write echo $apiContent it's shows the whole string but when i try to decode json string it's shows nothing

print_r(json_decode($apiContent, TRUE));

here is my whole function

function saveImportComplaint($xp)
{       
        $apiContent = $this->db->from("users_api")->get()->row()->apicontent;               
        return json_decode($apiContent, TRUE);      
}

After adding makejsonsafe function i can read and convert json, error coming because of unicode value

function makejsonsafe($myjsonstring)
{
    for ($i = 0; $i <= 31; ++$i) 
    {
        $myjsonstring = str_replace(chr($i), "", $myjsonstring);
    }
    $myjsonstring = str_replace(chr(127), "", $myjsonstring);
    if (0 === strpos(bin2hex($myjsonstring), 'efbbbf')) 
    {
        $myjsonstring = substr($myjsonstring, 3);
    }
    $myjsonstring = json_decode( $myjsonstring );
    return $myjsonstring;
}
  • 写回答

1条回答 默认 最新

  • dtbhp60824 2018-02-25 07:53
    关注

    It looks like you are dealing with:

    1. a Byte Order Mark
    2. and potentially some unicode / zero-width characters

    Without seeing your actual input string, I can't confidently write a perfect pattern or test my code, but my method should accomplish what your posted workaround does...

    function makejsonsafe($myjsonstring){
        return json_decode(preg_replace('/[\x00-\x1F\x7F]|^\xEF\xBB\xBF/u','',$myjsonstring));
        // return the data as an object
    }
    

    Pattern Breakdown:

    [                #start character class
        \x00-\x1F    #match range between "null" and "unit separator"
        \x7F         #match "delete"
    ]                #end character class
    |                #or
    ^\xEF\xBB\xBF    #match your BOM at start of string
    

    All that said, a shorter pattern may do the same trick:

    \p{Cc} is the same as [\x00-\x1F\x7F], so you might use: /\p{Cc}|^\xEF\xBB\xBF/u

    \P{C} will only target printable characters, so you might use the inverse: /\p{C}+/u

    References:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化