簡體   English   中英

json_decode 顯示沒有數據

[英]json_decode showing no data

我正在使用以下邏輯使用 PHP 在 MySQL 中以 JSON 格式存儲數據。

foreach ($_POST['data'] as $key => $value)
    {
        if($value[1] == "page_keywords")
            $store .= json_encode(array($value[1] => $value[2]));
        else
            $store .= json_encode(array($value[1] => trim($value[2])));
    }

session_start();
$date = new Date();
$modified = $date->getDate();

$query = ' UPDATE pages SET last_updated_user_author_id = "'.$_SESSION['user_id'].'", data = "'.htmlentities($store, ENT_QUOTES).'", modified = "'.$modified.'" WHERE id = "'.$pageID.'" ';

然后在解碼數據時我使用以下邏輯:

$query = ' SELECT data FROM pages WHERE id = "'.$_POST['pageID'].'" ';
$connection = $this->establish_connection();
$data = $connection->query($query);
$connection->close();
if($data->num_rows > 0)
    {
        while($row = $data->fetch_assoc())
            {
                $var = html_entity_decode($row['data']);
                echo json_decode($var);
            }
    }

雖然 json_decode 它沒有顯示任何數據作為響應,但當我做 var_dump 時它顯示為空,但如果我沒有做 json_decode 並且只使用 html_entity_decode() 我得到低於輸出

{"page_base_url":"http://www.myblog.com/about/contact/"}{"page_url_revision":"http://www.myblog.com/about/contact/"}{"page_url_alternate":"http://www.myblog.com/about/contact/"}{"page_url_shortlink":"http://www.myblog.com/about/contact/"}{"page_url_canonical":"http://www.myblog.com/about/contact/"}{"page_title":"Example | Contact"}{"page_name":"Example Contact"}{"page_type":"WebSite"}{"page_meta_description":"Want to get in touch with us? You're on the correct page, you can get in touch with us by filling the form below. We will get in touch with you with 24 hours."}{"page_keywords":["example","contact","support","help","getintouch","feedback","bug","updates"]}

我不確定我哪里出錯了,有人可以幫我嗎?

我想以 json_encode 格式給出一個 eccho 作為對 ajax 調用的響應。 我使用以下邏輯來這樣做

echo json_encode(
                array(
                        "type" => "error",
                        "status" => "Error While Retrieving Data!",
                        "message" => $error
                     )
            );

我認為你需要這樣的東西:

$store = array();
foreach ($_POST['data'] as $key => $value)
    {
        if($value[1] == "page_keywords")
            $store[] = array($value[1] => $value[2]);
        else
            $store[] = array($value[1] => trim($value[2]));
    }

$save = json_encode($store);

甚至(如果您的 $value[1] 在循環中始終是唯一的)

$store = array();
foreach ($_POST['data'] as $key => $value)
    {
        if($value[1] == "page_keywords")
            $store[$value[1]] = $value[2];
        else
            $store[$value[1]] = trim($value[2]);
    }

$save = json_encode($store);

然后使用 $save 存儲在您的表中。 不過,我不是 100% 的。

您顯示的字符串不是有效的 JSON。 如果你想以 JSON 格式存儲這樣的對象列表,它們需要在一個數組中,並用逗號分隔。 否則,它們只是不相關的單個對象,無法解碼為單個 JSON 塊。

因此,您需要在 PHP 中構建一個數組,然后在最后對整個內容進行編碼。 像這樣的東西:

$storedata = array();
foreach ($_POST['data'] as $key => $value)
{
    if($value[1] == "page_keywords")
        $storedata[] = array($value[1] => $value[2]);
    else
        $storedata[] = array($value[1] => trim($value[2]));
}

$jsondata = json_encode($storedata);

然后在您的 SQL 語句中使用$jsondata

您的問題是您“保存”到數據庫中。 您將每個鍵值對編碼為自己的 json-string 並連接這些 json-strings。

你的片段

foreach ($_POST['data'] as $key => $value)
{
    if($value[1] == "page_keywords")
        $store .= json_encode(array($value[1] => $value[2]));
    else
        $store .= json_encode(array($value[1] => trim($value[2])));
}

收益$store = "{key1:value1}{key2:value3}{key3:value3}" 請注意所有括號,您的字符串中有 3 個不同的 json 對象,其中包含一個鍵值對,而不是一個帶有 3 個鍵值對的 json 對象。

但是,我假設您想要一個帶有鍵值對的單個 json 對象,就像下面這樣?

$store = "{
    key1:value1,
    key2:value2,
    key3:value3
}";

如果是這樣,您需要以不同的方式構建數組:

$store = array();
foreach ($_POST['data'] as $key => $value)
{
    if($value[1] == "page_keywords")
        $store[$value[1]] = $value[2];
    else
        $store[$value[1]] = trim($value[2]);
}

請注意,為了大家的安全,您的代碼容易受到 sql 注入的影響。 也請解決這個問題。

暫無
暫無

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

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