Dim data() As JArray
For Each famil As jvalue In familys
If famil.ToString = dr("unfamily") Then
data(familys.IndexOf(famil)).Add(dr("盒数")) 这里会出错。感觉的二维Jarry的用法错了,如何改?
Else
data(familys.IndexOf(famil)).Add(0)
End If
Next
源数据来自于表。
For Each dr As DataRow In dt.DataRows
Dim dr1 As DataRow = dt2.find("time='" & dr("time") & "' and unfamily='" & dr("unfamily") & "'")
If dr1 IsNot Nothing Then
Dim drnew As DataRow = DataTables("leveling").AddNew
drnew("time") = dr("time")
drnew("盒数") = dr("盒数") - dr1("盒数")
drnew("unfamily") = dr("unfamily")
Else
Dim drnew As DataRow = DataTables("leveling").AddNew
drnew("time") = dr("time")
drnew("盒数") = dr("盒数")
drnew("unfamily") = dr("unfamily")
End If
Next
For Each dr As DataRow In dt2.DataRows
Dim dr1 As DataRow = dt.find("time='" & dr("time") & "' and unfamily='" & dr("unfamily") & "'")
If dr1 Is Nothing Then
Dim drnew As DataRow = DataTables("leveling").AddNew
drnew("time") = dr("time")
drnew("盒数") = 0 - dr("盒数")
drnew("unfamily") = dr("unfamily")
End If
Next 这里对表格进行合并。
DataTables("leveling").save
For Each famil As String In DataTables("leveling").GetValues("unfamily")
familys.Add(famil)
Next 获取所有产品族
For Each dr As DataRow In DataTables("leveling").Select("", "time")
If times.Contains(format(dr("time"), "d")) = False Then
times.add(format(dr("time"), "d"))
End If 加入时间轴
For Each famil As jvalue In familys
If famil.ToString = dr("unfamily") Then
data(familys.IndexOf(famil)).Add(dr("盒数"))
Else
MessageBox.Show(familys.IndexOf(famil)) 这里第一次会显示0,然后报错 未将对象引用设置到对象的实例。
data(familys.IndexOf(famil)).Add(0)
End If 将数量加入对应数组。
Next
Next
datas.Add(data)
jo("number") = datas
jo("date") = times
jo("familys") = familys
MessageBox.Show(1)
[此贴子已经被作者于2024/2/4 15:10:45编辑过]
表结构如图,
此主题相关图片如下:2.png
要的结果是:
jo:"date":[xxx,xxx,xxx], "familys":[1,2,3],"numbers":{[0,0,0,1],[0,1,0,0],[1,2,0,0]}
就是不同产品族的数量。numbers是一个二维数组。
[此贴子已经被作者于2024/2/4 15:16:57编辑过]
json和表数据对不上呀。麻烦根据5楼的数据,认真手工填一下json的真正需要的结果,发上来看看
jo:"date":[2023-12-10,2023-12-12,2023-12-21,2023-12-22], "familys":[其他,西可新],"numbers":{[157370,161540,0,0],[0,0,94990,95370]}
没看懂生成json的规则。
1、date数组是所有time列的数据,还是只是这4个?
2、familys数组是unfamily列所有不重复的值?
3、numbers数据根据什么规则生成的?
再说下面这种格式才是二维数组呀
"numbers":[[157370,161540,0,0],[0,0,94990,95370]]
搞定。
Dim familys, times, datas As New jarray
For Each famil As jvalue In familys
Dim data As New JArray
For Each dat As jvalue In times
Dim dr As DataRow = DataTables("leveling").find("time=#" & dat.tostring & "# and unfamily='" & famil.ToString & "'")
If dr IsNot Nothing Then
data.Add(dr("盒数"))
Else
data.Add(0)
End If
Next
datas.Add(data) 这里把每一个数量赋值到datas里面,dates就成为了二维数组。
Next