- 1、在WebApi中返回一個JSON格式的數據,如何到客戶端就變了
- 2、webapi讀取json webapi讀取json時,獲取的數據為空(webhook)
- 3、怎麼讓webapi返回json
轉載 在使用Web Api的時候,有時候只想返回JSON;實現這一功能有多種方法,本文提供兩種方式,一種傳統的,一種作者認為是正確的方法。
JSON in Web API – the formatter based approach
只支持JSON最普遍的做法是:首先清除其他所有的formatters,然後只保留JsonMediaTypeFormatter。
有了HttpConfiguration的實例,你將會很簡單的清除所有formatters,然後重新添加JsonMediaTypeFormatter。
實現代碼如下:
configuration.Formatters.Clear();
configuration.Formatters.Add(new JsonMediaTypeFormatter());這種方式雖然可以實現功能,但是所有的conent negotiation還是會發生,這就會產生以下額外的開銷了。因為,你已經知道要返回的結果了,也只想返回Json,其他的content negotiation都不需要了。
下面的方法可以很好的解決這個問題。
JSON in Web API – the conneg based approach
最好的方法是使用自定義的只返回Json Result的content negotiation代替Web Api中默認的content negotiation。
Conneg通過實現IContentNegotiator的Negotiator方法實現擴展。Negotiator方法返回ContentNegotiationResult(它包裝了你選擇的headers和formatter)。
下面的方法通過傳遞一個JsonMediaTypeFormatter給自定義的conneg negotiator,讓它一直返回applicaton/json 的content-type以及JsonMediaTypeFormatter。這種方法避免了每次請求都要重新創建一次formatter。
代碼如下:
public class JsonContentNegotiator : IContentNegotiator
{
private readonly JsonMediaTypeFormatter _jsonFormatter;
public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
{
_jsonFormatter = formatter;
}
public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerableMediaTypeFormatter formatters)
{
var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue(“application/json”));
return result;
}
}接下來,你需要在HttpConfiguration實例上註冊你的新的實現機制:
var jsonFormatter = new JsonMediaTypeFormatter();
//optional: set serializer settings here
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
通過替換默認的DefaultContentNegotiator,我們使用我們自定義的JsonContentNegotiator,它只支持Json,而且可以馬上返回。
如果你想更深入的了解Content Negotiation的知識,你可以查看作者的這篇文章。
總結
通過使用自定義的JsonContentNegotiator替換系統默認的DefaultContentNegotiator,很好的實現Web Api只返回Json的功能,而且沒有額外的開銷。
轉載 在使用Web Api的時候,有時候只想返回JSON;實現這一功能有多種方法,本文提供兩種方式,一種傳統的,一種作者認為是正確的方法。
JSON in Web API – the formatter based approach
只支持JSON最普遍的做法是:首先清除其他所有的formatters,然後只保留JsonMediaTypeFormatter。
有了HttpConfiguration的實例,你將會很簡單的清除所有formatters,然後重新添加JsonMediaTypeFormatter。
實現代碼如下:
configuration.Formatters.Clear();
configuration.Formatters.Add(new JsonMediaTypeFormatter());這種方式雖然可以實現功能,但是所有的conent negotiation還是會發生,這就會產生以下額外的開銷了。因為,你已經知道要返回的結果了,也只想返回Json,其他的content negotiation都不需要了。
下面的方法可以很好的解決這個問題。
JSON in Web API – the conneg based approach
最好的方法是使用自定義的只返回Json Result的content negotiation代替Web Api中默認的content negotiation。
Conneg通過實現IContentNegotiator的Negotiator方法實現擴展。Negotiator方法返回ContentNegotiationResult(它包裝了你選擇的headers和formatter)。
下面的方法通過傳遞一個JsonMediaTypeFormatter給自定義的conneg negotiator,讓它一直返回applicaton/json 的content-type以及JsonMediaTypeFormatter。這種方法避免了每次請求都要重新創建一次formatter。
代碼如下:
public class JsonContentNegotiator : IContentNegotiator
{
private readonly JsonMediaTypeFormatter _jsonFormatter;
public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
{
_jsonFormatter = formatter;
}
public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerableMediaTypeFormatter formatters)
{
var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue(“application/json”));
return result;
}
}接下來,你需要在HttpConfiguration實例上註冊你的新的實現機制:
var jsonFormatter = new JsonMediaTypeFormatter();
//optional: set serializer settings here
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
通過替換默認的DefaultContentNegotiator,我們使用我們自定義的JsonContentNegotiator,它只支持Json,而且可以馬上返回。
如果你想更深入的了解Content Negotiation的知識,你可以查看作者的這篇文章。
總結
通過使用自定義的JsonContentNegotiator替換系統默認的DefaultContentNegotiator,很好的實現Web Api只返回Json的功能,而且沒有額外的開銷。
返回的時候封裝成json即可。
參考: Student st1 = new Student(1, “dg”, 18, new Date());
Student st2 = new Student(2, “dg”, 18, new Date());
Student st3 = new Student(3, “dg”, 18, new Date());
Student st4 = new Student(4, “dg”, 18, new Date());
Student st5 = new Student(5, “dg”, 18, new Date());
List li = new ArrayList();
JSONObject JO1 = new JSONObject(st1);
JSONObject JO2 = new JSONObject(st2);
JSONObject JO3 = new JSONObject(st3);
JSONObject JO4 = new JSONObject(st4);
JSONObject JO5 = new JSONObject(st5);
li.add(JO1);
li.add(JO2);
li.add(JO3);
li.add(JO4);
li.add(JO5);
JSONArray Ja = new JSONArray(li);
Map ma = new HashMap();
ma.put(“Result”, “OK”);
ma.put(“Records”, Ja);
JSONObject js = new JSONObject(ma);
out.print(js);
返回結果:
{“Result”:”OK”,”Records”:[{“recordDate”:”Fri Dec 16 17:54:39 CST 2011″,”name”:”dg”,”age”:18,”personId”:1},{“recordDate”:”Fri Dec 16 17:54:39 CST 2011″,”name”:”dg”,”age”:18,”personId”:2},{“recordDate”:”Fri Dec 16 17:54:39 CST 2011″,”name”:”dg”,”age”:18,”personId”:3},{“recordDate”:”Fri Dec 16 17:54:39 CST 2011″,”name”:”dg”,”age”:18,”personId”:4},{“recordDate”:”Fri Dec 16 17:54:39 CST 2011″,”name”:”dg”,”age”:18,”personId”:5}]}
可以將存放Java對象的 列表 直接轉化為 json 數組 或對象,不必要過分麻煩的轉換。
從數據庫取出後換:
SourceDao sd = new SourceDao();
JSONArray ja = new JSONArray(sd.query(content));
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/126191.html