简体   繁体   中英

Issue In JSON Result when using JsonConvert.SerializeObject C#

I am getting bellow result when serializing the C# Model into JSON object.

This is my model.

public class ResultSet 
{
public int RowsInserted { get; set; }
public string RequestStatus { get; set; }
}

In my controller, I am creating an object to the model and assigning the values.

{
     ResultSet objResultSet = new ResultSet(); 
     objResultSet.RowsInserted = result;
     objResultSet.RequestStatus = "SuccessFul";
}

When returning the result I used the JsonConvert.Serialize

  return JsonConvert.SerializeObject(objResultSet);

I am getting the JSON result like the bellow :

"{\\"RowsInserted\\":1,\\"RequestStatus\\":\\"SuccessFul\\"}"

The actual result I am expecting is:

{"RowsInserted":1,"RequestStatus":"SuccessFul"}

I resolved this by changing the return type of method to the class instead of a string like this.

New method:

public ResultSet getData()
{
     ResultSet objResultSet = new ResultSet(); 

     objResultSet.RowsInserted = result;
     objResultSet.RequestStatus = "SuccessFul";

     return objResultSet;
}

Old Method

public string getData()
{
     ResultSet objResultSet = new ResultSet(); 

     objResultSet.RowsInserted = result;
     objResultSet.RequestStatus = "SuccessFul";

     return JsonConvert.SerializeObject(objResultSet);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM