c# - 如何序列化嵌套的 JSON?

标签 c# 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",
                    }),

                });

我得到以下输出: {"命令":"测试","参数":"{\"命令\":\"测试\"}"}

最佳答案

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

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; }
}

关于c# - 如何序列化嵌套的 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31535105/

相关文章:

c# - 如何在基本的 MVC5.1 网站中正确注册 AutoFac?

javascript - 尝试将 SVG 对象的 javascript 数组保存到 SQL

sql - 如何在 Oracle SQL Developer 中编辑 BLOB(包含 JSON)?

c# - 如何将面向数据类型的 DataTemplate 与 GridView 一起使用?

C# SQL 导出格式

javascript - 使用 HTML 5 File API 加载 JSON 文件

json - 如何获取所选操作表选择器Swift的索引路径

javascript - 为什么 JSON.parse() 会抛出 Uncaught SyntaxError : Unexpected token when trying to parse a simple json object?

c# - 如何声明一个未启动的任务,将等待另一个任务?

c# - 将 POST FromBody 转换为 HttpRequestMessage - 需要对请求正文进行反序列化