简体   繁体   English

Newtonsoft.Json C# :: 格式化 JsonConvert.SerializeObject

[英]Newtonsoft.Json C# :: Formatting JsonConvert.SerializeObject

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我正在使用以下内容生成 Json

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

This returns me following Json,这让我跟随 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,这很好,但我将其发送到 Twilio API,它具有以下格式的要求,

{
  "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?所以我的问题是,我需要在我的类/NewtonSoft 中进行哪些更改才能在单独的花括号中获取每个属性 [say&listen]?

Since your class is already called Actions , you could do something like this:由于您的类已经被称为Actions ,您可以执行以下操作:

[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);

A solution using Newtonsoft.Json:使用 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 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能在wcf服务中使用Newtonsoft.Json JsonConvert.SerializeObject? - Why I can't use Newtonsoft.Json JsonConvert.SerializeObject in wcf service? 使用JsonConvert.SerializeObject C#时JSON结果中的问题 - Issue In JSON Result when using JsonConvert.SerializeObject C# NewtonSoft JsonConvert.SerializeObject中的MaxJsonLength错误 - MaxJsonLength error in NewtonSoft JsonConvert.SerializeObject Newtonsoft.Json.JsonReaderException:在 JsonConvert.SerializeObject 之后解析 JsonConvert.DeserializeObject 时遇到意外字符 - Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing on JsonConvert.DeserializeObject after JsonConvert.SerializeObject C#:带有 JsonConvert.SerializeObject() 的 Dapper 无法正常工作 - C#: Dapper with JsonConvert.SerializeObject() not working properly JsonConvert.SerializeObject与json_encode - JsonConvert.SerializeObject vs json_encode JsonConvert.SerializeObject(本); 在JSON中呈现函数的结果 - JsonConvert.SerializeObject(this); to render the results of a function in the JSON JsonConvert.SerializeObject()导致JSON格式错误 - JsonConvert.SerializeObject() causing JSON to be malformed 使用JsonConvert.SerializeObject创建Json结构 - Create Json structure using JsonConvert.SerializeObject NewtonSoft JsonConvert.SerializeObject, \" 在序列化 DateTime 后添加 - NewtonSoft JsonConvert.SerializeObject, \" added after serializing DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM