【问题标题】:How to insert complex json file into mysql database using php?如何使用 php 将复杂的 json 文件插入 mysql 数据库?
【发布时间】:2019-10-05 08:20:39
【问题描述】:

我有一个包含多个键和值的 json 文件。有些值是字符串,有些是数组和对象。如何使用 php 将该 json 文件以 json 格式插入 mysql 数据库。

[
{
    "wordNumber": 40,
    "word": "Vocabulary 40",
    "meaning": [
        {"form": "adjective","values": ["Lorem Ipsum is not simply random text.","It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout."]}
    ],
    "partsOfSpeech": ["adjective"],
    "otherForms": [
        {"form": "noun","words": ["Adequacy"]},
        {"form": "adverb","words": ["Adequately"]}
    ],
    "synonyms": [
        {"form": "Adjective","words": ["Acceptable","All right","Secent","Qualified","Satisfactory","Suitable","Tolerable","Sufficient","Competent"]}
    ],
    "antonyms": [
        {"form": "Adjective","words": ["Insufficient","Deficient","lacking","Inadequate","Unacceptable","Unsatisfactory","Unqualified","Unsuitable"]}
    ],
    "hintletters": ["a","u","q"],
    "categories": {
        "section": 4,
        "group": 2
    },
    "examples": [
        {"form": "adjective","values": ["Employees did not have adequate training.","This car is adequate for our family.","Total amount of workers are adequate for this project."]}
    ]
},{
    "wordNumber": 41,
    "word": "Vocabulary 41",
    "meaning": [
        {"form": "adjective","values": ["Lorem Ipsum is not simply random text.","It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout."]}
    ],
    "partsOfSpeech": ["adjective"],
    "otherForms": [
        {"form": "noun","words": ["Adequacy"]},
        {"form": "adverb","words": ["Adequately"]}
    ],
    "synonyms": [
        {"form": "Adjective","words": ["Acceptable","All right","Secent","Qualified","Satisfactory","Suitable","Tolerable","Sufficient","Competent"]}
    ],
    "antonyms": [
        {"form": "Adjective","words": ["Insufficient","Deficient","lacking","Inadequate","Unacceptable","Unsatisfactory","Unqualified","Unsuitable"]}
    ],
    "hintletters": ["a","u","q"],
    "categories": {
        "section": 5,
        "group": 1
    },
    "examples": [
        {"form": "adjective","values": ["Employees did not have adequate training.","This car is adequate for our family.","Total amount of workers are adequate for this project."]}
    ]
}
]

我已经尝试过,但是当我将数据插入数据库时​​,我的所有数组和对象都变成了字符串,并且它们显示为“数组”这个词。我以后无法访问这些数据。

我已经使用这个 php 代码将数据插入数据库 -

<?php 

$connect = mysqli_connect("localhost","User","password","databaseName");

$fileName = "data2.json";

$data = file_get_contents($fileName);

$array = json_decode($data, true);

foreach($array as $row)
{
    $sql = "INSERT INTO vocabulary_data(wordNumber,word,meaning,partsOfSpeech,otherForms,synonyms,antonyms,hintletters,categories,examples) VALUES('".$row["wordNumber"]."','".$row["word"]."','".$row["meaning"]."','".$row["partsOfSpeech"]."','".$row["otherForms"]."','".$row["synonyms"]."','".$row["antonyms"]."','".$row["hintletters"]."','".$row["categories"]."','".$row["examples"]."')";

    mysqli_query($connect, $sql);
}

echo "All data inserted";

?>

我已使用此 PHP 代码从数据库中访问我的 json 数据 -

<?php 

$connect = mysqli_connect("localhost","User","password","databaseName");

$result = mysqli_query($connect, "SELECT * from data_table");
$json_array = array();

while($row = mysqli_fetch_assoc($result))
{
    $json_array[] = $row;
}

print(json_encode($json_array));

?>


But

当我访问数据时,它向我显示了这个问题。

    [{"wordNumber":"40","word":"Vocabulary 
40","meaning":"Array","partsOfSpeech":"Array","otherForms":"Array",
"synonyms":"Array","antonyms":"Array","hintletters":"Array",
"categories":"Array","examples":"Array"},{"wordNumber":"41",
"word":"Vocabulary 41",
"meaning":"Array","partsOfSpeech":"Array","otherForms":"Array",
"synonyms":"Array","antonyms":"Array","hintletters":"Array",
"categories":"Array","examples":"Array"}]

请问,谁能帮忙解决这个问题?

【问题讨论】:

  • 查看您的 JSON,meaning 将在解码后成为一个数组。你想为它存储什么? JSON 值?

标签: javascript php mysql arrays json


【解决方案1】:

您正在尝试将数组传递到 DB 字段。 如果要将 json 存储到 db 中,则需要通过 json_encode($array_name) 传递,它将再次将您的数组转换为 json 并将 stre 作为字符串转换为 db。

       $sql = "INSERT INTO vocabulary_data(wordNumber,word,meaning,partsOfSpeech,otherForms,synonyms,antonyms,hintletters,categories,examples) VALUES('".$row["wordNumber"]."','".$row["word"]."','".json_encode($row["meaning"])."','".json_encode($row["partsOfSpeech"])."','".json_encode($row["otherForms"])."','".json_encode($row["synonyms"])."','".json_encode($row["antonyms"])."','".json_encode($row["hintletters"])."','".json_encode($row["categories"])."','".json_encode($row["examples"])."')";

将json存入db后,你会得到json格式,如果你需要使用它,你需要从db中获取它并使用它。

将 json 存储在 DB 中后,您可能无法将过滤器应用到 db。

【讨论】:

  • 非常感谢您的帮助。它奏效了,我需要多一点帮助。如何从数据库中获取数据?我已使用此代码但无法正常工作 - 请问,你能给我正确的代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 2013-05-17
  • 1970-01-01
相关资源
最近更新 更多