如何在ASP.NET Web API中接收JSON? [英] How to receive json in ASP.NET web api?

查看:180
本文介绍了如何在ASP.NET Web API中接收JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一家外部公司将数据推送到我们的一台服务器,他们将发送JSON数据.我需要创建一个POST api来接收它.这是我到目前为止所拥有的

I have an external company pushing data to one of our server, they are going to send JSON data. I need to create a POST api to receive it. This is what I have so far

[System.Web.Http.HttpPost]
[System.Web.Http.ActionName("sensor")]
public void PushSensorData(String json)
{
    string lines = null;
    try
    {
          System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
          file.WriteLine(json);
          file.Close();
          //JSONUtilities.deserialize(json);
    }
    catch (Exception ex)
    {
        MobileUtilities.DoLog(ex.StackTrace);
    }
}

我正在通过使用提琴手发送json数据来测试它,但是json为null. 这是来自提琴手的原始数据.

I am testing it by send json data using fiddler but json is null. This is the raw data from fiddler.

POST http://localhost:31329/mobileapi/pushsensordata/ HTTP/1.1
User-Agent: Fiddler
Host: localhost:31329
Content-Type: application/json
Content-Length: 533

{
"id": {
    "server": "0.test-server.mobi",
    "application": "test-server.mobi",
    "message": "00007-000e-4a00b-82000-0000000",
    "asset": "asset-0000",
    "device": "device-0000"
},
"target": {
    "application": "com.mobi"
},
"type": "STATUS",
"timestamp": {
    "asset": 0000000
    "device": 00000000,
    "gateway": 000000,
    "axon_received_at": 00000,
    "wits_processed_at": 000000
},
"data_format": "asset",
"data": "asset unavailable"
}

推荐答案

在Web API中,您可以让框架为您完成大部分繁琐的序列化工作.首先将您的方法修改为:

In Web API you let the framework do most of the tedious serialization work for you. First amend your method to this:

[HttpPost]
public void PushSensorData(SensorData data)
{
    // data and its properties should be populated, ready for processing
    // its unnecessary to deserialize the string yourself.
    // assuming data isn't null, you can access the data by dereferencing properties:
    Debug.WriteLine(data.Type);
    Debug.WriteLine(data.Id);
}

创建一个类.为您起步:

Create a class. To get you started:

public class SensorData 
{
    public SensorDataId Id { get; set; }
    public string Type { get;set; }
}

public class SensorDataId
{
    public string Server { get; set; }
}

此类的属性需要镜像JSON的结构.我将它留给您完成向模型中添加属性和其他类的步骤,但是按照书面形式,该模型应该可以工作.不符合您的模型 的JSON值将被丢弃.

The properties of this class need to mirror the structure of your JSON. I leave it to you to finish adding properties and other classes to your model, but as written this model should work. The JSON values that don't correspond to your model should be thrown out.

现在,当您调用Web API方法时,传感器数据将已经反序列化.

Now, when you call your Web API method, your sensor data will already be deserialized.

有关更多信息,请参见 http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

For more information, see http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

这篇关于如何在ASP.NET Web API中接收JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆