V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
fankcoder
V2EX  ›  PHP

求教这种数据怎么转为 json

  •  
  •   fankcoder · Jul 9, 2020 · 4342 views
    This topic created in 2119 days ago, the information mentioned may be changed or developed.
    array (
      0 => 
      array (
        'desc_image' => '/Public/uploads/images/2018/03/20180328222626124.jpg',
        'desc_image_title' => '485494001647432084',
      ),
      1 => 
      array (
        'desc_image' => '/Public/uploads/images/2018/03/20180328222644137.jpg',
        'desc_image_title' => 'DFJ_3445',
      )
    )
    

    用 Py 重构老的 PhP 系统,数据库里看到这种数据不知道咋处理了。。

    12 replies    2020-07-10 01:04:46 +08:00
    faraone
        1
    faraone  
       Jul 9, 2020
    建议使用脚本把数据库数据规整一下
    winnerczwx
        2
    winnerczwx  
       Jul 9, 2020
    全部转成对象吧;

    {
    "0": {
    "desc_image: "xxx"
    "desc_image_title": "xxx"
    }
    }
    hui314
        3
    hui314  
       Jul 9, 2020 via Android
    array_value()是你想要的么?
    lscexpress
        4
    lscexpress  
       Jul 9, 2020
    json_encode
    airdge
        5
    airdge  
       Jul 9, 2020
    json.dumps(eval(re.sub(r"\s*\)",'}',数组数据.replace('=>',':').replace('array (','{'))))
    hejingyuan199
        6
    hejingyuan199  
       Jul 9, 2020   ❤️ 1
    数据库里存这样的数据?
    这不是 php 的 array 类型吗?

    我觉得我会用一种很土的办法,
    先用 php 调用 json_encode()
    然后再入库存为 json 。
    一劳永逸。

    反正你只是改一次,
    改好后以后也不会用 php 了。
    areless
        7
    areless  
       Jul 9, 2020 via Android
    py 比 php 还慢。只要写一个 array 函数,将=>替换成=就可以了。
    gitjavascript
        8
    gitjavascript  
       Jul 9, 2020
    数据库重新洗一下吧
    fankcoder
        9
    fankcoder  
    OP
       Jul 9, 2020
    @airdge 感谢大佬!!
    fankcoder
        10
    fankcoder  
    OP
       Jul 9, 2020
    @hejingyuan199 感谢回复,是这个思路,写个脚本全换 json 。
    Pursue9
        11
    Pursue9  
       Jul 9, 2020
    [{"desc_imag":"/Public/uploads/images/2018/03/20180328222626124.jpg",
    "desc_image_title":"485494001647432084"
    },{"desc_imag":"/Public/uploads/images/2018/03/20180328222626124.jpg",
    "desc_image_title":"485494001647432084"
    }]
    leoleoasd
        12
    leoleoasd  
       Jul 10, 2020
    我觉得最不容易出问题的方式, 是 php 写个脚本转换数据...
    ```php
    eval("$a=".$str.";");
    echo json_encode($a);
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5651 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 72ms · UTC 08:35 · PVG 16:35 · LAX 01:35 · JFK 04:35
    ♥ Do have faith in what you're doing.