Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);
    }
}

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"
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
280 views
Welcome To Ask or Share your Answers For Others

1 Answer

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; }
}

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.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...