簡體   English   中英

如何將提取的Json數據導入數據庫

[英]How do i import the extracted Json data to database

我正在嘗試從omdbapi獲取電影信息。到目前為止,我有這段代碼是使用omdb api從imdb提取數據的,但是我想將該數據導入到我的數據庫中。我該如何做到這一點。

我的代碼看起來像這樣

    <form>
<input type="text"  id="tst"/>
<input type="button" value="search"  id="btn"/>
</form>
<table class="table table-hover" id="imdb">
        <thead>
            <tr>
                <th>Poster</th>
                <th>Title</th>
                <th>Year</th>
                <th>Rated</th>
                <th>Runtime</th>
                <th>Genre</th>
                <th>Director</th>
                <th>Actors</th>
                <th>Plot</th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>

這是我用來獲取電影信息的jQuery代碼

$(document).ready(function () {
          $('#btn').click(function(){
              var imdbid=$('#tst').val();
               var url = "http://www.omdbapi.com/?i="+imdbid+"&plot=short&r=json"
        $.ajax({
        url:url,
        dataType:'json',
        success:function (json) {
            var tr;
        tr = $('<tr/>');
        tr.append("<td><img src=" + json.Poster + " width='200' height='297'></td>");
        tr.append("<td>" + json.Title + "</td>");
        tr.append("<td>" + json.Year + "</td>");
        tr.append("<td>" + json.Rated + "</td>");
        tr.append("<td>" + json.Runtime + "</td>");
        tr.append("<td>" + json.Genre + "</td>");
        tr.append("<td>" + json.Director + "</td>");
        tr.append("<td>" + json.Actors + "</td>");
        tr.append("<td>" + json.Plot + "</td>");
        $('#imdb').append(tr);
        }
        })
              })
    });

您可以從api響應中創建一個json對象(與創建html的功能相同)。

 JSONData = JSON.stringify({
            "Title": Title,
            "Year": Year,
            "Rated" : Rated,
            ...........so on

        });
postDataToserver(JSONData);

您可以定義函數來發布數據,例如-

function postDataToserver(JSONData)
    $.ajax({
        url: "your_server_script_path",
        type:"POST",
        data:JSONData,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function(response) {
        error: function (error) {

        }
    });

如果要以簡單的帖子格式發送而不是作為JSON對象發送,則無需使用JSON.stringify,並且類似地,在函數中刪除contentype信息。 希望這會幫助你。

暫無
暫無

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

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