C#利用JSON保存控件数据时JARRAY中前面元素被后面元素覆盖掉如何解决?
1.94K 次浏览
RT,执行保存控件数据的部分代码如下
//定义JSON数组对象
JArray JAA=new JArray() {"","","","","","","",""};
//定义JSON对象
JObject JO2 = new JObject();
//--DEL
int tb_n = 0;
/*定义控件名字变量*/
string cnname = "";
/*定义控件名字数组 */
string[] cnnamearr = new string[3];
//遍历控件
foreach (Control co in this.Controls)
{
cnname = co.Name;
if (Regex.IsMatch(cnname, "^TeaSetingsTBox([\\d]{1})([_][ABC])$"))
{
cnnamearr=cnname.Split(new char[2] { 'x','_'});
JO2["ID"]=cnnamearr[1];
switch (cnnamearr[2])
{
case "A":
if (Regex.IsMatch(co.Text, "^([0-9]{1,})$"))
JO2["A"] = int.Parse(co.Text);
else
JO2["A"] = float.Parse(co.Text);
break;
case "B":
if (Regex.IsMatch(co.Text, "^([0-9]{1,})$"))
JO2["B"] = int.Parse(co.Text);
else
JO2["B"] = float.Parse(co.Text);
break;
case "C":
if (Regex.IsMatch(co.Text, "^([0-9]{1,})$"))
JO2["C"] = int.Parse(co.Text);
else
JO2["C"] = float.Parse(co.Text);
break;
default:break;
}
tb_n = int.Parse(cnnamearr[1]) - 1;
JAA[tb_n ]= JO2;
tb_n = 0;
}
}
控件的命名是TeaSetingsTBox1_A ,TeaSetingsTBox1_B,TeaSetingsTBox1_C …这样子,现在的问题是我无法保存最开始的JAA的一个元素,他总是被后续的覆盖掉,例如执行完前三次的 JAA[tb_n ]= JO2; JAA[7]里面的值为{“ID”:”8”,”C”:8,”B”:8,”A”:8},但是,执行完前6次后JAA[7]里面就变成了{“ID”:”7”,”C”:7,”B”:7,”A”:7},前9次JAA[7]后就成了{“ID”:”6”,”C”:6,”B”:6,”A”:6},一直到最后JAA[7]变成了{“ID”:”1”,”C”:1,”B”:1,”A”:1}并且和JAA[0]一样,请问下这是哪里的问题,增加一个JAA的元素和改变JAA元素的保存顺序(从7-0保存或从0-7保存)都不能解决问题,但除了这个JAA[7]之外,JAA[6]-JAA[0]都是正常的。使用的是nuget里面的Newtonsoft.Json.
1 个回答
-
用一个集合对象存储不同控件的信息就可以了。
示例程序代码如下:
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace ConsoleApp1 { internal class Program { private static void Main(string[] args) { var controls = new List<Control> { new() { Name = "TeaSetingsTBox1_A", Text = "1" }, new() { Name = "TeaSetingsTBox1_B", Text = "2" }, new() { Name = "TeaSetingsTBox1_C", Text = "3" }, new() { Name = "TeaSetingsTBox2_A", Text = "4" }, new() { Name = "TeaSetingsTBox2_B", Text = "5" }, new() { Name = "TeaSetingsTBox2_C", Text = "6" }, new() { Name = "TeaSetingsTBox3_A", Text = "7" }, new() { Name = "TeaSetingsTBox3_B", Text = "8" }, new() { Name = "TeaSetingsTBox3_C", Text = "9" } }; var result = new List<Item>(); foreach (var co in controls) { var cnname = co.Name; if (Regex.IsMatch(cnname, "^TeaSetingsTBox([\\d]{1})([_][ABC])$")) { var cnnamearr = cnname.Split('x', '_'); var item = new Item { Id = cnnamearr[1] }; switch (cnnamearr[2]) { case "A": if (Regex.IsMatch(co.Text, "^([0-9]{1,})$")) //JO2["A"] = int.Parse(co.Text); item.A = int.Parse(co.Text); else //JO2["A"] = float.Parse(co.Text); item.A = float.Parse(co.Text); break; case "B": if (Regex.IsMatch(co.Text, "^([0-9]{1,})$")) //JO2["B"] = int.Parse(co.Text); item.B = int.Parse(co.Text); else item.B = float.Parse(co.Text); break; case "C": if (Regex.IsMatch(co.Text, "^([0-9]{1,})$")) item.C = int.Parse(co.Text); else item.C = float.Parse(co.Text); break; } result.Add(item); } } Console.WriteLine(JsonConvert.SerializeObject(result)); Console.ReadKey(); } public class Item { public string Id { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public object A { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public object B { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public object C { get; set; } } public class Control { public string Name { get; set; } public string Text { get; set; } } } }
运行结果:
[{"Id":"1","A":1},{"Id":"1","B":2},{"Id":"1","C":3},{"Id":"2","A":4},{"Id":"2","B":5},{"Id":"2","C":6},{"Id":"3","A":7},{"Id":"3","B":8},{"Id":"3","C":9}]