-1

Im trying to return detailed feedback in my API so created a class:

public class Notify
{
   public class Root
   {
     Public string Error {get; set;}
     Public Details Details {get; set;}
   }

  public class Details
   {
     public string FeedbackText {get; set;}
     ....
   }
}

In my class i populate the object with

public JObject NotifyUser()
{
     var r = new Notify.Root 
     { 
     Error = "Some error",
     Details = new Notify.Details
           { 
             FeedbackText = "Something to do"
            }
     };

     return JObject.Parse(DoSomethingWithJson(r));
}

DoSomethingWithJson is a string method and passing in the object to modify the data.

In my API i have

var currentValue = NotifyUser(); // I see the Json and values in this variable but next line doesnt display the values only the Json fields
return StatusCode(HttpStatusCode.BadRequest, currentValue);

but it just returns the JSON fields without any data against any field. I convert it to a string and everything appears including the text assigned to each field but the response is listed as text and not JSON.

Ive tried converting the code to JsonConvert.SerialiseObject but if its in a string format it shows the data otherwise its empty properties (i would like the response to be encoded as json).

Im not sure why its doing this? What have i missed? I did search around but most were targeting old versions so i feel im doing something wrong here.

Computer
  • 2,149
  • 7
  • 34
  • 71
  • 1
    This won't compile. There's no string to parse in `NotifyUser` either. What are you trying to do? If you want to convert `r` to a JSON string, use `JsonConvert.Serialize(r)`. JSON is text with a specific format, not a type like `JObject`. That's what JSON.NET uses to allow LINQ queries over JSON. – Panagiotis Kanavos Jun 08 '23 at 15:35
  • @Computer Do you think it is very smart and respectfull to add DoSomethingWithJson(r) to your post without showing the code? – Serge Jun 08 '23 at 18:31
  • *but it just returns the JSON fields without any data against any field.* -- this could be because you are actually using System.Text.Json. See e.g. [How can I serialize a Newtonsoft JToken to JSON using System.Text.Json?](https://stackoverflow.com/q/65386083) or [ASP.Net core - blank response when returning a JObject property](https://stackoverflow.com/q/72224818). But please provide a [mcve] so we can confirm. – dbc Jun 08 '23 at 19:04

1 Answers1

1

Your code can not be even compilled. You can use Parse only for a json string. But you have a c# object. You should try to compile your code before asking the question. The last line should be

return JObject.FromObject(r);

But probably you don't need to convert it to JObject since APi action will automatically serialize it to a json string. It doesnt matter if it is a json object or c# object.

Serge
  • 40,935
  • 4
  • 18
  • 45
  • It definitely compiled otherwise no reason for me to post the question as the error would give it away. I may have had a typo above but it compiled before i even posted – Computer Jun 08 '23 at 15:30
  • @Computer it didn't. `Parse` expects a string, `r` is a `Root`. There's no string in the code you posted. You probably pasted the wrong snippet – Panagiotis Kanavos Jun 08 '23 at 15:31
  • Sorry i miss typed - ive amended my original post. I tried FromObject but considering i missed off the key part of the code, the above didnt work - apologies – Computer Jun 08 '23 at 15:42
  • @Computer fixed your question – Serge Jun 08 '23 at 15:43