2

I've traced through the my method (below) at run time. Below the code I've included a screenshot of the Debug examination of the Portfolios list just before the return statement. I've expanded a couple of the list entries. As you can see, each Portfolio object contains bona fide data - a numeric Id and a string name.

Yet, here's what comes back to the Chrome browser, or postman, no difference.

[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]

What's happening to all the data? Thanks for your help!

 // GET: api/Portfolio
 [HttpGet]
 public List<Portfolio> Get()
 {
 List<Portfolio> Portfolios = new List<Portfolio>();
 using (SqlConnection cn = new SqlConnection(conn))
 {
 SqlCommand cmd = cn.CreateCommand();
 cmd.CommandText = sqlSelectPortfolios;
 cn.Open();
 SqlDataReader rdr = cmd.ExecuteReader();
 Portfolio pf;
 while (rdr.Read())
 {
 pf = new Portfolio
 {
 id = (int)rdr["PortfolioId"],
 name = (string)rdr["PortfolioName"]
 };
 Portfolios.Add(pf);
 }
 }
 return Portfolios;
 }

Debug:

Debug Screenshot

asked Nov 10, 2019 at 1:56
3
  • 1
    If id and name are fields and you're using System.Text.Json.JsonSerialiser than it won't serialise them as of Nov 2019. Commented Nov 10, 2019 at 2:03
  • 1
    Does this answer your question? How to use class fields with System.Text.Json.JsonSerializer? Commented Nov 10, 2019 at 2:04
  • What is your Portfolio class code? Commented Nov 10, 2019 at 3:03

1 Answer 1

9

The id and name need to be public and properties. As of 3.0 System.Text.Json.JsonSerializer does not serialize fields.

public class Portfolio 
{
public int id {get; set;}
public string name {get; set;}
}
answered Nov 10, 2019 at 2:07
2
  • Thank you!! I had them as public but not defined as properties. As soon as I redefined these as properties I got the expected results. Commented Nov 10, 2019 at 3:36
  • 3
    The issue is logged as JsonSerializer should support field as well as properties #36505 Commented Nov 10, 2019 at 3:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.