我有两个相同长度的数组,我想使用这些内容。当我保存到json文件时,数组a和b的index [0]应该一起使用。我猜我需要通过它们来访问它们以便每次访问每个索引并将内容保存到模板json。我还希望它返回json的toString()格式,因此每个索引的每次迭代都会返回一些内容。
public string Show(string[] id, string[] msg)
{
// opening code for json file with jobject and jsontexreader
for (int i = 0; i <= id.Length; i++ )
{
Newid = id[i];
Newmsg = msgs[i];
// setting the data to the json file
JObject temp = (JObject)o1.SelectToken(path1);
temp["data"] = msg;
JObject tem = (JObject)o1.SelectToken(path2);
tem["ksid"] = id;
}
return ??;
}
答案 0 :(得分:1)
以下是完成您想要做的事情的简单示例
string Show(string[] id, string[] msg)
{
if (id.Length != msg.Length)
throw new Exception(nameof(id) + " is not the same length as " + nameof(msg));
List<object> data = new List<object>();
for (int i = 0; i < id.Length; i++)
{
data.Add(new
{
Ksid = id[i],
Data = msg[i]
});
}
return Newtonsoft.Json.JsonConvert.SerializeObject(data);
}