“{“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();
}