c#写入生成Json格式文本

“{“header”:{“head1”:“head1”,“head1”:“head1”},“body”:{“head1”:“head1”,“head1”:“head1”},“result”:{“head1”:“head1”,“head1”:“head1”}}”

应用下文代码可以得到以上结果;

using Newtonsoft.Json;
        static List<string> headerNameList = new List<string>();
        static List<string> headerValueList = new List<string>();
        static List<string> BodyNameList = new List<string>();
        static List<string> BodyValueList = new List<string>();
        static List<string> StaBodyNameList = new List<string>();
        static List<string> StaBodyValueList = new List<string>();
        static List<string> AlrmBodyNameList = new List<string>();
        static List<string> AlrmBodyValueList = new List<string>();
        static List<string> ResultNameList = new List<string>();
        static List<string> ResultValueList = new List<string>();

 public static string writetest()
        {
            StringWriter sw = new StringWriter();
            JsonWriter JsonWriter = new JsonTextWriter(sw);
            JsonWriter.WriteStartObject();
            if (headerNameList.Count > 0)

       {

        JsonWriter.WritePropertyName("header");
        JsonWriter.WriteStartObject();
        for (int i = 0; i < headerNameList.Count; i++)
        {
            JsonWriter.WritePropertyName(headerNameList[i]);
            JsonWriter.WriteValue(headerValueList[i]);
        }
        JsonWriter.WriteEndObject();
    }
    if (BodyNameList.Count > 0)
    {
        JsonWriter.WritePropertyName("body");
        JsonWriter.WriteStartObject();
        for (int i = 0; i < BodyNameList.Count; i++)
        {
            JsonWriter.WritePropertyName(BodyNameList[i]);
            JsonWriter.WriteValue(BodyValueList[i]);
        }
        JsonWriter.WriteEndObject();
    }
    if (ResultNameList.Count > 0)
    {

        JsonWriter.WritePropertyName("result");
        JsonWriter.WriteStartObject();
        for (int i = 0; i < ResultNameList.Count; i++)
        {
            JsonWriter.WritePropertyName(ResultNameList[i]);
            JsonWriter.WriteValue(ResultValueList[i]);
        }
        JsonWriter.WriteEndObject();
    }
    JsonWriter.WriteEndObject();
    JsonWriter.Flush();
    return sw.GetStringBuilder().ToString();
}
        private void button40_Click(object sender, EventArgs e)
        {
            headerNameList = new List<string>();
            headerValueList = new List<string>();
            BodyNameList = new List<string>();
            BodyValueList = new List<string>();
            ResultNameList = new List<string>();
            ResultValueList = new List<string>();
            headerNameList.Add(  "head1");
            headerValueList.Add("head1");
            BodyNameList.Add("head1");
            BodyValueList.Add("head1");
            ResultNameList.Add("head1");
            ResultValueList.Add("head1");
            headerNameList.Add("head1");
            headerValueList.Add("head1");
            BodyNameList.Add("head1");
            BodyValueList.Add("head1");
            ResultNameList.Add("head1");
            ResultValueList.Add("head1");
            var ret = writetest();
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值