簡體   English   中英

如何拆分和更改json格式

[英]How to split and change the json format

我正在使用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"
}];

可能嗎? 謝謝

我不確定這是否完全符合您的要求,但是您可以通過將原始JSON轉換為關聯數組,然后使用Foreach()循環將每次迭代的數據重組為新的assoc數組,來獲得所需的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"
}]

編輯-更新的答案

如注釋中所述,您將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() .'}}';
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM