Json.net不带名称的动态括号



您是否可以使用动态json创建括号,而不在json字符串中显示列表/数组的名称?否则,我将需要重写整个部分。

问题是,我还没有弄清楚如何在dymanic json中创建括号,而不在括号前面加上名称。在我的json字符串中,值将有一个父括号来包装所有条目,然后是一个括号来包装时间戳,另一个括号包括书籍括号包装时间戳&书的前面不应该出现名字

使用这个链接,我非常接近我想要的格式,但我错过了时间和剩余条目的括号分隔。

示例代码:

JObject jsonobject = new JObject();
JObject jsonobject2 = new JObject();
dynamic topObject = jsonobject;
topObject.records = new JArray() as dynamic;
dynamic midObject = jsonobject2;
midObject.value = new JArray() as dynamic;
dynamic record = new JObject();
record.TIMESTAMP = DateTime.Now.ToString("MMMM dd yyyy HH:mm:ss");
record.ID = "sampleid";
midObject.value.Add(record);
//query for data
//loop through query
// add to record when data found ex:record.author=dr[0].ToString();
// midObject.value.Add(record);
// finished add data from query
topObject.records.Add(midObject);
string jsonstr = topObject.ToString();    

电流输出:

{
"records": [
{
"value": [
{
"TIMESTAMP": "January 03 2017 09:46:15",
"ID": "sampleID"
},
{
"Title": "Book2",
"Author": "author1"
},
{
"Title": "Book1"
"Author": "author1"
"Notes": "testtest"
}
]
}
]
}

期望输出:

{
"records": [
{
"value": [
[ ------->bracket wrapping timestamp
{
"TIMESTAMP": "January 03 2017 09:46:15",
"ID": "sampleID"
}
] ------->bracket wrapping timestamp
,
[ ------->bracket wrapping books 
{
"Title": "Book2",
"Artist": "artist1"
},
{
"Title": "Book1"
"Artist": "artist1"
"Notes": "testtest"
}
] ------->bracket wrapping books 
] 
}
]
}

编辑:可能过于简单了,但图书部分有20多个字段。每个json字符串只有一个时间戳,但每个请求可以有100到1000本书。

第2版:标题/对主要问题的澄清。

据我所见,您只需要为所有记录添加另一个数组:

JObject jsonobject = new JObject();
JObject jsonobject2 = new JObject();
dynamic topObject = jsonobject;
topObject.records = new JArray() as dynamic;
dynamic midObject = jsonobject2;
midObject.value = new JArray() as dynamic;
dynamic arrayrecord = new JArray();
dynamic record = new JObject();
record.TIMESTAMP = DateTime.Now.ToString("MMMM dd yyyy HH:mm:ss");
record.ID = "sampleid";
arrayrecord.Add(record);
midObject.value.Add(arrayrecord);
//query for data
//loop through query
// add to record when data found ex:record.author=dr[0].ToString();
// midObject.value.Add(record);
// finished add data from query
topObject.records.Add(midObject);
string jsonstr = topObject.ToString();

相关内容

  • 没有找到相关文章

最新更新