webapi解析json(webapi介紹)

  • 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-hk/n/126191.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
簡單一點的頭像簡單一點
上一篇 2024-10-03 23:07
下一篇 2024-10-03 23:07

相關推薦

  • JSON的MD5

    在Web開發過程中,JSON(JavaScript Object Notation)是最常用的數據格式之一。MD5(Message-Digest Algorithm 5)是一種常用…

    編程 2025-04-29
  • 使用Java將JSON寫入HDFS

    本篇文章將從以下幾個方面詳細闡述Java將JSON寫入HDFS的方法: 一、HDFS簡介 首先,先來了解一下Hadoop分佈式文件系統(HDFS)。HDFS是一個可擴展性高的分佈式…

    編程 2025-04-29
  • 如何使用Newtonsoft datatable轉Json

    Newtonsoft DataTable 是一個基於.NET的JSON框架,也是一個用於序列化和反序列化JSON的強大工具。 在本文中,我們將學習如何使用Newtonsoft Da…

    編程 2025-04-28
  • JPRC – 輕鬆創建可讀性強的 JSON API

    本文將介紹一個全新的 JSON API 框架 JPRC,通過該框架,您可以輕鬆創建可讀性強的 JSON API,提高您的項目開發效率和代碼可維護性。接下來將從以下幾個方面對 JPR…

    編程 2025-04-27
  • Python存為JSON的方法及實例

    本文將從以下多個方面對Python存為JSON做詳細的闡述。 一、JSON簡介 JSON(JavaScript Object Notation)是一種輕量級的數據交換格式,易於人閱…

    編程 2025-04-27
  • 使用Python獲取JSON並解析

    本文將介紹如何使用Python獲取JSON數據並解析相關內容。通過使用Python的第三方庫,我們可以輕鬆地處理JSON數據,包括讀取、提取和操作JSON數據。 一、獲取JSON數…

    編程 2025-04-27
  • 使用Spread 8展示JSON數據

    使用Spread 8可以方便地展示JSON數據,本文將詳細介紹如何利用Spread 8展示JSON數據。 一、Spread 8簡介 Spread 8是一款強大的電子表格軟件,可以方…

    編程 2025-04-27
  • 如何在json轉實體類時忽略大小寫

    本文將從以下幾個方面介紹如何在json轉實體類時忽略大小寫。 一、使用Gson庫實現json轉實體類忽略大小寫 Gson是Google提供的Java JSON操作庫,它提供了簡單易…

    編程 2025-04-27
  • C# 中 JSON null 不顯示的處理方法

    本文將為大家介紹在 C# 中處理 JSON null 不顯示的解決方法。 一、null 不顯示的問題 在使用 C# 進行 JSON 數據處理的時候,經常會遇到 null 值不顯示的…

    編程 2025-04-27
  • Hive解析JSON詳解

    一、JSON簡介 JSON(JavaScript Object Notation)是一種輕量級的數據交換格式,具有結構清晰、易於讀寫、便於解析等特點。它基於JavaScript的一…

    編程 2025-04-25

發表回復

登錄後才能評論