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 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码