0

I have an app. It's frontend is set on Angular 6 using typescript controllers and it's backend on a C# api.

I try to send json from C# controller as response but typescript controller cannot read it successfully.

C# code:

  [HttpGet]
  [Route("getAll")]
  public string GetAllUserStories()
    { 
          .
          .
          .

  string json = JsonConvert.SerializeObject(storiesList);

        json = "\"" + json + "\"";

        return json;
    }

Story class :

 public class Story
{
    /// <summary>
    /// The story short description.
    /// </summary>
    public string description { get; set; }

    /// <summary>
    /// The story id.
    /// </summary>
    public int Id { get; set; }

    /// <summary>
    /// The list of places this story contains.
    /// </summary>
    public string place { get; set; }

    /// <summary>
    /// The list of friends travelled with on this story.
    /// </summary>
    public string friends { get; set; }

    /// <summary>
    /// The story's photo.
    /// </summary>
    public Photo Photo { get; set; }

    /// <summary>
    /// The day this story started.
    /// </summary>
    public string date { get; set; }

    /// <summary>
    /// The day this story ended.
    /// </summary>
    public DateTime EndingDate { get; set; }

    /// <summary>
    /// The amount of money wasted on this story.
    /// </summary>
    public string cost { get; set; }

    /// <summary>
    /// How much the user liked this story.
    /// </summary>
    public string rating { get; set; }

    /// <summary>
    /// A short comment at the end of the story.
    /// </summary>
    public string Conclusion { get; set; }

    /// <summary>
    /// Story image.
    /// </summary>
    public string image { get; set; }

  }

Typescript controller:

        const stories = 
 this.http.get("https://localhost:44341/stories/getAll").subscribe(results => {this.retGetData= results as AllStories;});  

And I receive on console on browser : SyntaxError: Unexpected token d in JSON at position 4 text: ""[{"description":"test description","Id":10,"place":"test place","friends" ....etc

How can I cast a list of objects to Json so it would be readable from Angular??

Grigoris Loukidis
  • 383
  • 2
  • 7
  • 23
  • 1
    It looks like you are using WebAPI that already construct JSON for you... Just returning your data as is should be enough (as shown in what I think is reasonable [duplicate](https://stackoverflow.com/questions/35937118/build-json-response-in-web-api-controller)). If that is not enough - [edit] your post with correct tags and explanation how default JSON serialization did not work. – Alexei Levenkov Apr 08 '19 at 17:48
  • I will test it Alexei! Thank you – Grigoris Loukidis Apr 08 '19 at 17:58

0 Answers0