「[[マイクロソフト系技術情報 Wiki>http://techinfoofmicrosofttech.osscons.jp/]]」は、「[[Open棟梁Project>https://github.com/OpenTouryoProject/]]」,「[[OSSコンソーシアム .NET開発基盤部会>https://www.osscons.jp/dotNetDevelopmentInfrastructure/]]」によって運営されています。 -[[戻る>JSON]] * 目次 [#c8385b30] #contents *概要 [#q09445f8] [[WCF]] や [[ASP.NET Web API]] で、指定の JSON を受ける(受信する)方法を説明する。 *JSONを受信するサービスを作成する [#fb585173] **[[WCF]] の場合 [#e09891e6] ***以下のように[[WCF]]を定義する。 [#w7b3d356] [[WebAPI>#s4811ff5]]と同様、[[WCF]]でもイイ感じにBindingしてくれる。 -KeyValRequestがPOSTされたJSONで、 -ソレ以外のパラメタはQueryString [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class JSONService : IJSONService { public StringResponse PostAnswersKeyValue( KeyValRequest[] akv, string userName, string enterpriseID, string storeID, string deviceID, string screenID, string initializeScreenInfoID, string time) { ・・・ // 基本的に正常系の戻り値を返す。 return new StringResponse() { IsError = isError, Message = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") }; } } StringResponseでJSONも返せる。 ***呼び出し側の実装(JQuery) [#mc8d6de5] function PostJsonFromWebStorage(url) { // Web Storageのすべての情報の取得 var jsonArray = new Array(); for (var i = 0; i < storage.length; i++) { var _key = storage.key(i); // Web Storageのキーと値を表示 var jsonBean = { key: _key, value: storage.getItem(_key) }; jsonArray.push(jsonBean); } // <p id="url"></p> に表示 if (document.getElementById("url") != null) { $("#url").text(url); } // <p id="request"></p> に表示 if (document.getElementById("request") != null) { $("#request").text("request:" + JSON.stringify(jsonArray).toString()); } CallService("POST", url, JSON.stringify(jsonArray), "application/json; charset=utf-8", "JSON", false); } // --------------------------------------------------------------- // ajax // --------------------------------------------------------------- // 引数 // Type : GET or POST or PUT or DELETE verb // Url : Location of the service // Data : Data sent to server // ContentType : Content type sent to server // DataType : Expected data format from server // ProcessData : True or False // 戻り値 - // --------------------------------------------------------------- function CallService(Type, Url, Data, ContentType, DataType, ProcessData) { $.ajax({ type: Type, url: Url, data: Data, cache: false, contentType: ContentType, dataType: DataType, processdata: ProcessData, success: function (data) { // On Successfull service call ServiceSucceeded(data); }, error: function (data) { // When Service call fails ServiceFailed(data); } }); } // --------------------------------------------------------------- // $.ajaxのコールバック(success) // --------------------------------------------------------------- // 引数 data // 戻り値 - // --------------------------------------------------------------- function ServiceSucceeded(data) { // Success if (document.getElementById("response") != null) { // <p id="response">response</p> に結果を表示 $("#response").text("response-success:" + JSON.stringify(data).toString()); } ClearWebStorage(); // 送信成功のため、WebStorageをクリア } // --------------------------------------------------------------- // $.ajaxのコールバック(error) // --------------------------------------------------------------- // 引数 data // 戻り値 - // --------------------------------------------------------------- function ServiceFailed(data) { // Error if (document.getElementById("response") != null) { // <p id="response">response</p> に結果を表示 $("#response").text("response-error:" + JSON.stringify(data).toString()); } // 送信失敗のため、WebStorageをクリアしない。 } **[[ASP.NET Web API]] の場合 [#s4811ff5] ---- Tags: [[:.NET開発]], [[:通信技術]], [[:.NET Standard]], [[:.NET Core]], [[:ASP.NET]], [[:ASP.NET Web API]]