0

In My code, I am trying to receive an API containing a JSON body containing data, I have designed a DataInfo Model class and used the [FromBody] function to translate the data.

My issue is that the data being pulled which I have formatted to be ints and doubles, is being pulled as a series of 0s

My Data Info Model Class:

public class DataInfo
{
    [JsonProperty("ID")]
    public int JId { get; set; }
    [JsonProperty("Temp")]
    public double JTemperature { get; set; }
    [JsonProperty("Humidity")]
    public double JHumidity { get; set; }
    [JsonProperty("WindSpeed")]
    public double JWindSpd { get; set; }
    
    [JsonProperty("SoilMoisture")]
    public double JSM { get; set; }
}

} My Api Controller: [HttpPatch("update")]

    public async Task<ActionResult<Device>> Update([FromBody] DataInfo data)
    {
      

        var dev = await _dbContext.Device.Where(x => x.Id == data.JId).FirstOrDefaultAsync();
        if (dev == null)
        {
            return BadRequest("no device found");
        }
        dev.Humidity = data.JHumidity;
        dev.Temp = data.JTemperature;
        dev.WindSpeed = data.JWindSpd;
        dev.SoilMoisture = data.JSM;
        dev.DateTime = DateTime.Now;
        _dbContext.SaveChanges();
        return Ok(dev);
    }

My JsonBody: {

"ID":"1",
"Temp":"37",
"Humidity":"42",
"WindSpeed":"12",
"SoilMoisture":"14" 

} I also Used this JSON body and got the same result:

{ "data":{

"ID":"1",
"Temp":"37",
"Humidity":"42",
"WindSpeed":"12",
"SoilMoisture":"14" 

}

}

When I run the API call in postman, I receive a 400 error with the text that correlates to an invalid Device ID, when I had it send the Device ID and other data points within the class, all were returned as 0s.

The API call is going to the correct controller, however the JSON data seems only to be exsisting as 0s even though I assign it with the JsonPropertyName or JsonProperty assignment in the model class.

  • 2
    don't post your code as images, paste it as text – Maku Dec 05 '22 at 19:54
  • 2
    You have a root element of "data" in your json and then the properties according to your DataInfo class. Means you are missing a surrounding class with the one "data" property of type DataInfo. That class should be then used in the signatire of the endpoint method. – Ralf Dec 05 '22 at 19:58
  • Your json looked different in the picture to the now shown json text. Is the shown json correct? – Ralf Dec 05 '22 at 19:59
  • This is my first Stack overflow question and I am figuring out how to ask questions at the same time! sorry! – AHartwig99 Dec 05 '22 at 20:02
  • 1
    The first json, without the surrounding "data" looks correct to me. You didn't intermix the json serialization you use here? Like using "System.Text.Json" for serialization but the JsonProperty Attribute from NewtonSoft.Json for example? – Ralf Dec 05 '22 at 20:09
  • 1
    As a troubleshooting step, I suggest temporarily changing it to `[FromBody] string stringData` and dump the string to a log. We need to know for sure the input is correct. Then you can try `var dev = JsonConvert.DeserializeObject(stringData)` in code to see if it converts. This should shed some light on the problem. – John Wu Dec 05 '22 at 20:09

1 Answers1

0

I found the issue to be in My Using inputs. I was including Newtonsoft which was conflicting with system.

using BlazorApp1.Data;
using BlazorApp1.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.EntityFrameworkCore;
//using Newtonsoft.Json;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;