0

I have following classes,

  public class Actions
        {
            public string say { get; set; }
            public bool? listen { get; set; }
        }

        public class ActionsWrapper 
        { 
            public List<Actions> actions { get; set; }
            public ActionsWrapper(string say, bool listen = false)
            {
                this.actions = new List<Actions>();

                var action = new Actions();
                action.say = say;
                action.listen = listen;
                this.actions.Add(action);
            }
        }

And I am using the following to generate Json

var actions = new ActionsWrapper(say: "Hi, how can I help you today?");
return JsonConvert.SerializeObject(actions);

This returns me following Json,

{"actions":[
      {
        "say":"Hi, how can I help you today?",
         "listen": false
       }
]}

Which is good, but I am sending this to Twilio API which has the requirement of the following format,

{
  "actions": [
    {
      "say": "Hi, how can I help you today?"
    },
    {
      "listen": false
    }
  ]
}

So my question is, what changes do I need in my classes / NewtonSoft to get each property [say&listen] in separate curly braces?

3
  • 1
    Wrap it in a separate class Commented Mar 29, 2020 at 10:25
  • 1
    "separate curly brackets" in JSON means it is an object. In your class, they are string and bool values. You can create classes for each of them. Or you could use JSON converter. Commented Mar 29, 2020 at 10:25
  • [ ] indicates an array of objects... Commented Mar 29, 2020 at 12:09

2 Answers 2

1

A solution using Newtonsoft.Json:

public class Say
{
    public string say { get; set; }
}

public class Listen
{
    public bool? listen { get; set; }
}

public class ActionsWrapper
{
    public List<Say> Says { get; set; }
    public List<Listen> Listens { get; set; }
    public ActionsWrapper(string say, bool listen = false)
    {
        this.Says = new List<Say>();
        this.Listens = new List<Listen>();
        Says.Add(new Say() { say = say });
        Listens.Add(new Listen() { listen = listen });
    }
}

Usage:

var actions = new ActionsWrapper(say: "Hi, how can I help you today?");

JArray JArraySays = JArray.FromObject(actions.Says);
JArray JArrayListens = JArray.FromObject(actions.Listens);
JArraySays.Merge(JArrayListens);

return JsonConvert.SerializeObject(new { actions = JArraySays });
Sign up to request clarification or add additional context in comments.

1 Comment

yep this works. ``` {"actions":[{"say":"Hi, how can I help you today?"},{"listen":false}]} ```
1

Since your class is already called Actions, you could do something like this:

[Serializable]
public class Actions : ISerializable
{
    public string say { get; set; }
    public bool? listen { get; set; }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("actions", new object[] { new { say }, new { listen } });
    }
}

Usage:

var actions = new Actions();
actions.say = say;
actions.listen = listen;
var json = JsonConvert.SerializeObject(actions);

2 Comments

Hi, implemented this, but it looks like it just gives output as mentioned in the question.
@KiranP. Sorry my bad, I forgot that [Serializable] attribute. Now updated. Also created a .net fiddle dotnetfiddle.net/lxxWKO

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.