【问题标题】:How to split and change the json format如何拆分和更改json格式
【发布时间】:2016-12-30 06:52:08
【问题描述】:

我正在使用 slimphp v2,我有以下功能

function gt($user) {
    $sql = "select id, id as categoryId, mobile, task from actions where status=0";
    try {
        $db = newDB($user);
        $stmt = $db - > prepare($sql);
        $stmt - > execute();
        $gs = $stmt - > fetchAll(PDO::FETCH_OBJ);
        if ($gs) {
            $db = null;
            echo json_encode($gs, JSON_UNESCAPED_UNICODE);
        } else {
            echo "Not Found";
        }
    } catch (PDOException $e) {
        echo '{"error":{"text":'.$e - > getMessage().
        '}}';
    }
}

默认的 json 输出如下所示:

[{
    "id": "1",
    "categoryId": "1",
    "mobile": "111",
    "task": "test"
},
{
    "id": "2",
    "categoryId": "2",
    "mobile": "222",
    "task": "test2"
}]

以及我想要制作的输出。我需要生成一个 ID:1_(id) 然后像这样格式化它

[{
    id: "1",
    task: "test",
}, {
    ID: "1_1", //generate this, add 1_id
    categoryId: "1",
    mobile: "00000",
},  {
    id: "2",
    task: "test2",
}, {
    ID: "1_2", //generate this, add 1_id
    categoryId: "2",
    mobile: "11111"
}];

有可能吗? 谢谢

【问题讨论】:

    标签: php json slim


    【解决方案1】:

    我不确定这是否正是您所追求的,但您可以通过将原始 JSON 转换为关联数组,然后使用 Foreach() 循环将每次迭代中的数据重构为新的关联数组,从而获得所需的 JSON 输出大批。之后,您可以使用 json_encode() 将其转换回 JSON。

    代码:

    $json = '[{
        "id": "1",
        "categoryId": "1",
        "mobile": "111",
        "task": "test"
    },
    {
        "id": "2",
        "categoryId": "2",
        "mobile": "222",
        "task": "test2"
    }]';
    
    $jsonArr = json_decode($json, TRUE);
    $newArr = [];
    
    foreach ($jsonArr as $v) {
    
        $newArr[] = ['id:'=>$v['id'], 'task:' => $v['task']];
        $newArr[] = ['ID:'=>'1_' . $v['id'], 'categoryId' => $v['categoryId'], 'mobile'=>$v['mobile']];  
    
    }
    
    $newJson = json_encode($newArr);
    var_dump($newJson);
    

    输出:

    [{
        "id:": "1",
        "task:": "test"
    }, {
        "ID:": "1_1",
        "categoryId": "1",
        "mobile": "111"
    }, {
        "id:": "2",
        "task:": "test2"
    }, {
        "ID:": "1_2",
        "categoryId": "2",
        "mobile": "222"
    }]
    

    编辑 -- 更新答案

    正如 cmets 中所讨论的,您将 SQL 数组作为对象输出。我使用PDO::FETCH_ASSOC 将Fetch 设置为作为关联数组输出,并将foreach() 循环更改为引用assoc 数组$gs。这应该可以,但如果没有,则再次使用var_dump($gs) 输出$gs 的结果。如果需要,您仍然需要编码为 JSON,但此行已被注释掉。

    function gt($user) {
        $sql = "select id, id as categoryId, mobile, task from actions where status=0";
            try {
                    $db = newDB($user);
                    $stmt = $db->prepare($sql);
                    $stmt->execute();
                    $gs = $stmt->fetchAll(PDO::FETCH_ASSOC); //Fetch as Associative Array
    
                    if ($gs) {
    
                        $db = null;
                        $newArr = [];
    
                        foreach ($gs as $v) { //$gs Array should still work with foreach loop
    
                            $newArr[] = ['id:'=>$v['id'], 'task:' => $v['task']];
                            $newArr[] = ['ID:'=>'1_' . $v['id'], 'categoryId' => $v['categoryId'], 'mobile'=>$v['mobile']];
    
                        }
    
                        //$newJson = json_encode($newArr); //JSON encode here if you want it converted to JSON.
    
    
    
    
                    } else {
    
                     echo "Not Found";
    
                    }
    
            } catch(PDOException $e) {
    
                //error_log($e->getMessage(), 3, '/var/tmp/php.log');
                    echo '{"error":{"text":'. $e->getMessage() .'}}';
            }
    }
    

    【讨论】:

    • 如果你注释掉我的代码和 var_dump($gs);你得到 JSON 输出了吗?你能把 JSON 放在你的问题中吗(编辑任何机密)
    • 我可以看到问题出在哪里,您尝试 json_decode() 和不是 JSON 的对象。为了测试,如果您在if() 语句中添加$gs = json_encode($gs); 作为第一行,那么该对象将进行json_decode 并且应该完成。显然这不是一个修复,所以让我知道它是否有效。
    • 好吧,让我试试
    • 我已更新我的答案以匹配您的代码。请参阅解释以获得充分理解,并让我知道您的进展情况。
    • 是的,这种方式就像一个魅力!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多