goodkidbadmAArky
goodkidbadmAArky

Reputation: 43

JSON is empty, using JsonResult Type in MVC

I'm trying to output a JSON file. I am using netcoreapp3.1 along with the Newtonsoft.Json NuGet. Here is my code where I enter all the information:

    private BlockWrapper GetBlock()
    {
        var blockWrapper = new BlockWrapper
        {
            blocks = new List<IntBlock>
            {
                new SectionBlockBlock
                {
                    text = new TextBlock
                    {
                        type = "mrkdwn",
                        text = "Hello! I am Multiuse Kat. How can I help you ?"
                    }
                },
                new ActionsBlockBlock
                {
                    elements = new ElementBlock[]
                    {
                        new ElementBlock
                        {
                            type = "button",
                            text = new TextBlock
                            {
                                type = "plain_text",
                                text = "Help"
                            },
                            style = "primary",
                            value = "click_me_123"
                        }
                    }
                },
            }
        };
        return blockWrapper;
    }

My models look like this. Notice I am using an interface with IntBlock:

public class BlockWrapper
{
    public List<IntBlock> blocks { get; set; }
}
public class SectionBlockBlock : IntBlock
{
    public string type { get; } = "section";
    public string blockId { get; set; }
    public TextBlock text { get; set; }
}
public class ActionsBlockBlock : IntBlock
{
    public string type { get; } = "actions";
    public string blockId { get; set; }
    public ElementBlock[] elements { get; set; }
}
public class TextBlock
{
    public string type { get; set; }
    public string text { get; set; }
    public bool? emoji { get; set; }
}
public class ElementBlock
{
    public string type { get; set; }
    public string action_id { get; set; }
    public TextBlock text { get; set; }
    public string value { get; set; }
    public string style { get; set; }
}


public interface IntBlock { }

When I return new JsonResult(GetBlock()); I output this:

enter image description here

I'm assuming this has something to do with the interface of IntBlock. Can someone tell me how to fix this? Thanks!

Upvotes: 0

Views: 143

Answers (1)

A Farmanbar
A Farmanbar

Reputation: 4798

Try use this

return JsonConvert.SerializeObject(GetBlock());

i do always use above approach and all work fine.

And also, Your interface properties are null

public interface IntBlock { }

Therefore, your collection type is empty interface and serializer wouldn't be able to find any property to serialize it.

i think below code should work

public class ActionsBlockBlock : IntBlock
{
    IntBlock.type { get; } = "actions";
    IntBlock.blockId { get; set; }
    IntBlock.ElementBlock[] elements { get; set; }
}
public interface IntBlock 
{ 
    public string type { get; set; }
    public string blockId { get; set; }
    public ElementBlock[] elements { get; set; }
}

do above schema for all of your class and try again . i think it works .

Upvotes: 1

Related Questions