温度/ GPSデータをクラウドブローカーに送信するデバイスがあります。次に、ブローカは、「HTTPは、デバイスのデータJSONオブジェクトを選択したURLにポストします」(ブローカのサイトのドキュメントから引用します)。
この投稿をC#でどのように受け取って逆シリアル化しますか?私はnewtonsoft.jsonを使用しています。私はstackoverflowから同様の例を見てきましたが、私にとっては多くのエラーを作り出しています。私のコードは以下の通りです。 publicクラスのRootオブジェクトは、JSONを貼り付けることで作成されたので、すべて自動でした。最後の部分は、私が何をするべきか分からないものです(パブリッククラステストセクション)。私はどこに行くのか、それを適切にフォーマットする方法はわかりません。
ありがとうございます。C#でHTTP Post JSONを受信して使用するasp.net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http;
using Newtonsoft.Json;
namespace WebServiceTest1.Models
{
public class Rootobject
{
public DateTime received { get; set; }
public string authtype { get; set; }
public string[] tags { get; set; }
public Routingresults routingResults { get; set; }
public string device_name { get; set; }
public int errorcode { get; set; }
public string source { get; set; }
public string timestamp { get; set; }
public string record_id { get; set; }
public string data { get; set; }
public int device_id { get; set; }
}
public class Routingresults
{
public int matchedRules { get; set; }
public object[] errors { get; set; }
}
//This is the part I dont know where to put, or how to use properly.
public class Testing
{
string url = "http://example.com/MethodThatReturnsJson";
//I'm sure this needs to be the URL of the site that is POSTING.
WebClient client = new WebClient();
string webServiceJsonString = client.DownloadString(url);
//This line is erroring out. doesn't like client.DownloadString(url)
Rootobject yourObject = JsonConvert.DeserializeObject<Rootobject>(webServiceJsonString);
//I typed Rootobject here based on class Rootobject above, but not confident that is correct.
}
}
}
ありがとうございます。
ブローカーがリクエストを送信するエンドポイントが必要ですか?または、いくつかのデータを取得するget要求を発行する必要がありますか? – peco
エンドポイントが必要です。ブローカーにはルールがあり、モニターからデータを受信すると直ちにメッセージを送信します。 – bairdmar