简体   繁体   English

使用ASP.NET Web API将JSON转换为HTML表

[英]JSON to HTML table using ASP.NET Web API

I have a functioning web api using .NET Web API. 我有一个使用.NET Web API的可运行的Web API。 The return is JSON data via the api url call /api/v1/Au/Get. 通过api网址调用/ api / v1 / Au / Get返回的是JSON数据。 Right now I don't know how to feed that data into something useful as this url path is my api call and not associated with a view. 现在,我不知道如何将数据输入有用的东西,因为此url路径是我的api调用,并且与视图无关。 How do I return data that I can pass into an HTML table or list? 如何返回可传递到HTML表或列表中的数据? I have been researching a lot and haven't come up with anything too useful. 我一直在研究很多,还没有想到任何有用的东西。

this is the form that makes the call 这是打电话的形式

<form class="rhc" method="POST" action="/api/v1/Au/Get">
    <input type="text" name="twitter_handle" class="form-control" placeholder="Twitter" />
    <span class="input-group-btn">
        <button class="btn btn-default" type="submit">
            <span class="ladda-label">Free Preview</span>
            <span class="ladda-spinner"></span>
        </button>
    </span>
</form>

The returned JSON contains a lot of data and the raw string is all that appears on the page after the submission of the form is made. 返回的JSON包含大量数据,并且原始字符串就是提交表单后在页面上显示的全部内容。 Any help on this would be so much appreciated. 在这方面的任何帮助将不胜感激。

The answer from the Web API is JSON, which is a JavaScript object serialized as string (JSON is JavaScript Object Notation). Web API的答案是JSON,这是一个序列化为字符串的JavaScript对象(JSON是JavaScript Object Notation)。 What you need to do is to 您需要做的是

  1. get the JSON answer form the server 从服务器获取JSON答案
  2. convert it to a JavaScript object 将其转换为JavaScript对象
  3. and use some kind of template or MV* framework to show it easyly in a table. 并使用某种模板或MV *框架将其轻松显示在表格中。

For 1, you need to make the query using AJAX to get the JSON response. 对于1,您需要使用AJAX进行查询以获取JSON响应。

For 2 you can use the JSON functionality of the Browser ( JSON.parse ) or a JSON library if the browser is too old (for example JSON 3 ). 对于2,您可以使用浏览器的JSON功能( JSON.parse ),或者使用JSON库(如果浏览器过旧)(例如JSON 3 )。

However, if you use a framework like jQuery to make the Ajax requests, you can directly get the JavaScript object. 但是,如果使用类似jQuery的框架来发出Ajax请求,则可以直接获取JavaScript对象。

For 3, you can use JavaScript template libraries like Handlebars or Mustache , or an MV* library like the MVVM Knockout.JS 对于3,您可以使用JavaScript模板库(如HandlebarsMustache) ,或MV *库(如MVVM Knockout.JS)。

Pseudocode: 伪代码:

$.post('service url', {twitter_handle: 'value'}).done(function(data) {
   // Use any of the template or MV* solution to generate the HTML
   // from the data object and the template
});

Alternatively, you can use any of the available grid JavaScript libraries, like the jQuery plugins datatables or SlickGrid or jqGrid . 另外,您可以使用任何可用的网格JavaScript库,例如jQuery插件datatablesSlickGridjqGrid

As you can see, there aa lot of options, but the basic idea is explained in the 3 points at the beginning of this answer. 如您所见,有很多选择,但是在此答案开头的三点中对基本思想进行了解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM