2017-02-23 108 views
-1

我正在開發Grails Concert應用程序,我正在使用AngularJS從bandsintown.com獲取實際事件。將JSON數據添加到MySQL數據庫

$.getJSON("http://api.bandsintown.com/events/search.json?&api_version=2.0&app_id=FYP&location=Dublin,Ireland", function(result) 

我能夠檢索事件,我現在希望把每個事件爲對象,並將其添加到我的數據庫,這樣我就可以有一個獨特的頁面與他們的後續細節和諸如此類的東西每個事件。

任何人都可以提供指導如何做這樣的事情?

謝謝!

+2

似乎是一個**寬泛的問題。 – Mistalis

+0

似乎是重複的:http://stackoverflow.com/questions/35041851/inserting-json-object-into-mysql-table-with-angular-php –

+0

使用'JSON.stringify(result);'並插入這個字符串在你的數據庫在各自的列。當閱讀使用'JSON.parse(jsonstring):' –

回答

0

我寫的東西,而前一個插件,它可以幫助你讓你的頭在你身邊需要做什麼:

private String viewTicketField(String jiraserver, String jirauser,String jirapass, String url, String customField) { 
    if (url.endsWith('undefined')) { 
     return 
    } 

    String output='' 

    try { 
     HBuilder hBuilder=new HBuilder() 
     RESTClient http = hBuilder.httpConn(jiraserver, jirauser, jirapass,httpConnTimeOut,httpSockTimeOut) 
     http.request(Method.GET) { req -> 
      uri.path = url 
      response.success = { resp, json -> 
       log.info "Process URL Success! ${resp.status} " 
       def result=JSON.parse(json.toString()) 
       if (result.fields."customfield_${customField}") { 
        output=result.fields."customfield_${customField}" 
       } 
      } 
      response.failure = { resp -> 
       log.error "Process URL failed with status ${resp.status}" 
      } 
     } 
    } 
    catch (HttpResponseException e) { 
     log.error "Failed error: $e.statusCode" 
    } 
    return output 
} 

HBuilder類可以在src

找到您已連接後回來的JSON響應,你再分析每一行

您創建一個新的域類

String url 
String datetime 
String app_id 
String ticket_url 

...等等,這是從上面的URL整個條目,以便爲你實際需要放於域類的字符串,然後分析每個迭代上面代碼中的每個關鍵

{"id":13926481,"url":"http://www.bandsintown.com/event/13926481?app_id=FYP","datetime":"2017-02-23T17:00:00","ticket_url":"http://www.bandsintown.com/event/13926481/buy_tickets?app_id=FYP\u0026came_from=233","artists":[{"name":"Rackhouse Pilfer","url":"http://www.bandsintown.com/RackhousePilfer","mbid":null}],"venue":{"id":3334537,"url":"http://www.bandsintown.com/venue/3334537","name":"Convention Centre","city":"Dublin","region":"07","country":"Ireland","latitude":53.3500292,"longitude":-6.2385286},"ticket_status":"unavailable","on_sale_datetime":null