如何序列化嵌套JSON?(How to serialize Nested JSON?)

系统教程 行业动态 更新时间:2024-06-14 16:57:39
如何序列化嵌套JSON?(How to serialize Nested JSON?)

我有以下嵌套的JSON:

{"Command":"helo", "parameter" : {"Configured":false, "ApplicationString":"Something", "Hostname":"some", "IPAddress":"0.0.0.0", "UniqueID":"", "Username":"me"}}

我需要将此字符串作为JSON对象传递给在C#中对我的Web服务进行POST调用。 有谁能帮我怎么做这一步?

注意:我能够传递简单的JSON,如下所示:

var request = (HttpWebRequest)WebRequest.Create("http://localhost:8084"); request.ContentType = "text/json"; request.Method = "POST"; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { string json = new JavaScriptSerializer().Serialize(new { Command = "test", name="pooja" }); streamWriter.Write(json); }

如果我按照相同的方式传递嵌套的json,如下所示:

string json = new JavaScriptSerializer().Serialize(new { Command = "test", parameter = new JavaScriptSerializer().Serialize(new { Command = "test", }), });

我得到以下输出:{“Command”:“test”,“parameter”:“{\”Command \“:\”test \“}”}

I have following nested JSON:

{"Command":"helo", "parameter" : {"Configured":false, "ApplicationString":"Something", "Hostname":"some", "IPAddress":"0.0.0.0", "UniqueID":"", "Username":"me"}}

And I need to pass this string as JSON object to POST call to my web service in C#. Could anyone help me how to do this step?

Note: I am able to pass simple JSON like below:

var request = (HttpWebRequest)WebRequest.Create("http://localhost:8084"); request.ContentType = "text/json"; request.Method = "POST"; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { string json = new JavaScriptSerializer().Serialize(new { Command = "test", name="pooja" }); streamWriter.Write(json); }

If I follow the same way to pass nested json like below :

string json = new JavaScriptSerializer().Serialize(new { Command = "test", parameter = new JavaScriptSerializer().Serialize(new { Command = "test", }), });

I get below output : {"Command":"test","parameter":"{\"Command\":\"test\"}"}

最满意答案

如果您有任何问题,请告诉我。

void Main() { CommandParamater exampleCommand = new CommandParamater { Command = "Foo", Parameter = new Parameter { ApplicationString = "App String Foo", Configured = true, Hostname = "Bar", IPAddress = "8.8.8.8", UniqueID = Guid.NewGuid().ToString(), Username = "FooBar" } }; string uri = "http://localhost:8084"; string data = JsonConvert.SerializeObject(exampleCommand); Html htmlClient = new Html(); htmlClient.Post(uri, data, "application/json"); } public class Html { public string Post(string uri, string data, string contentType) { byte[] dataBytes = Encoding.UTF8.GetBytes(data); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; request.ContentType = contentType; request.ContentLength = dataBytes.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(dataBytes, 0, dataBytes.Length); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { return reader.ReadToEnd(); } } } public class Parameter { [JsonProperty("Configured")] public bool Configured { get; set; } [JsonProperty("ApplicationString")] public string ApplicationString { get; set; } [JsonProperty("Hostname")] public string Hostname { get; set; } [JsonProperty("IPAddress")] public string IPAddress { get; set; } [JsonProperty("UniqueID")] public string UniqueID { get; set; } [JsonProperty("Username")] public string Username { get; set; } } public class CommandParamater { [JsonProperty("Command")] public string Command { get; set; } [JsonProperty("parameter")] public Parameter Parameter { get; set; } }

Let me know if you have any issues.

void Main() { CommandParamater exampleCommand = new CommandParamater { Command = "Foo", Parameter = new Parameter { ApplicationString = "App String Foo", Configured = true, Hostname = "Bar", IPAddress = "8.8.8.8", UniqueID = Guid.NewGuid().ToString(), Username = "FooBar" } }; string uri = "http://localhost:8084"; string data = JsonConvert.SerializeObject(exampleCommand); Html htmlClient = new Html(); htmlClient.Post(uri, data, "application/json"); } public class Html { public string Post(string uri, string data, string contentType) { byte[] dataBytes = Encoding.UTF8.GetBytes(data); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; request.ContentType = contentType; request.ContentLength = dataBytes.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(dataBytes, 0, dataBytes.Length); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { return reader.ReadToEnd(); } } } public class Parameter { [JsonProperty("Configured")] public bool Configured { get; set; } [JsonProperty("ApplicationString")] public string ApplicationString { get; set; } [JsonProperty("Hostname")] public string Hostname { get; set; } [JsonProperty("IPAddress")] public string IPAddress { get; set; } [JsonProperty("UniqueID")] public string UniqueID { get; set; } [JsonProperty("Username")] public string Username { get; set; } } public class CommandParamater { [JsonProperty("Command")] public string Command { get; set; } [JsonProperty("parameter")] public Parameter Parameter { get; set; } }

更多推荐

本文发布于:2023-04-13 12:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/6771b36ac3824286e0954daa39d40a36.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   序列化   JSON   serialize   Nested

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!