以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  如何遍历日志表,生成josn文件内容呢?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=152744)

--  作者:李孝春
--  发布时间:2020/7/20 16:07:00
--  如何遍历日志表,生成josn文件内容呢?
现有一表“日志”有如下字段和内容

图片点击可在新窗口打开查看此主题相关图片如下:1122.png
图片点击可在新窗口打开查看


Dim jo As New JObject
Dim ja As New JArray
jo("danweiname") = "AAA"
jo("name") = "BB"
jo("date") =  ja \'
ja.Add(New JObject) \'给数组添加两个对象成员
ja.Add(New JObject) 
ja(0)("time") = "2020-12-11"
ja(0)("context") = "下午"
ja(1)("time") = "2020-12-12"
ja(1)("context") = "中午"
Output.Show(jo.ToString)

上述代码可以固定生成
{
  "danweiname": "AAA",
  "name": "BB",
  "date": [
    {
      "time": "2020-12-11",
      "context": "下午"
    },
    {
      "time": "2020-12-12",
      "context": "中午"
    }
  ]
}


随着日志表记录越来越多  这个相对固定生成josn的形式就不大方便了

有没有办法实现如下功能呢?
1、根据日志表中的danweiname  name  time三个字段作为条件  分类生成context内容
2、先判断danweiname 如上josn结果 先显示单位名称  (若遇多个单位:需要先一个单位显示完毕了才生成下一个单位的)
3、判断name,如上josn结果  根据单位名称 显示name  (若遇一个单位,日志记录里有多个name值相同的,先显示完毕了才显示下一个name的相关内容)
4、判断time且满足danweiname name的条件时,如上josn结果  根据time先后顺序进行数据生成 并显示context的对应值

--  作者:有点蓝
--  发布时间:2020/7/20 16:29:00
--  
使用getvalues+select遍历即可

\'定义数组集合的时候,要在类型后加上括号,表示这是一个数组集合。
Dim
 Arys As List(Of String()) 
Arys = 
DataTables("订单").GetValues("danweiname|name"\'列名用符号|分割
\'注意循环变量是字符型数组,所以类型是String(),而不是String

For
 Each Ary As String() In Arys
    Output.Show(Ary(
0) & "|" & Ary(1)) 
for each dr as datarow in DataTables("订单").select("danweiname= \'" & Ary(0) & "\' and name=xxxx")
    Output.Show(dr(time)
next
Next

--  作者:李孝春
--  发布时间:2020/7/20 17:05:00
--  回复:(有点蓝)使用getvalues+select遍历即可'定义数...
Dim Arys As List(Of String())
Arys = DataTables("josn").GetValues("danweiname|name") \'列名用符号|分割
\'注意循环变量是字符型数组,所以类型是String(),而不是String
For Each Ary As String() In Arys
    Output.Show(Ary(0) & "|" & Ary(1))
        Dim jo As New JObject
        Dim ja As New JArray
        jo("danweiname") = Ary(0)
        jo("name") =  Ary(1)
        jo("date") =  ja \'
        ja.Add(New JObject) \'给数组添加两个对象成员
        ja.Add(New JObject)
    For Each dr As DataRow In DataTables("josn").Select("danweiname= \'" & Ary(0) & "\' and name=\'" & Ary(1) & "\'")
        Output.Show(dr("tiem"))
        Output.Show(dr("context"))
        ja(0)("tiem") = dr("tiem")
        ja(0)("context") = dr("context")
        ja(1)("tiem") = dr("tiem")
        ja(1)("context") = dr("context")
        Output.Show(jo.ToString)
    Next
Next

红色部分启用之后  会报错  不启用 则生成如下:
2012-12-12
下午
{
  "danweiname": "AAA",
  "name": "BB"
}
2020-11-11
中午
{
  "danweiname": "AAA",
  "name": "BB"
}
2020-11-11
WANSHANG
{
  "danweiname": "AAA",
  "name": "BB"
}
2012-11-12
CE
{
  "danweiname": "AAA",
  "name": "CC"
}
2012-11-13
ZHONGWU
{
  "danweiname": "BBB",
  "name": "QQ"
}

--  作者:有点蓝
--  发布时间:2020/7/20 17:13:00
--  
Dim Arys As List(Of String())
Arys = DataTables("josn").GetValues("danweiname|name") \'列名用符号|分割
\'注意循环变量是字符型数组,所以类型是String(),而不是String
Dim jaaa As New JArray
Dim jo,jo2 As JObject
For Each Ary As String() In Arys
    Output.Show(Ary(0) & "|" & Ary(1))
    jo = New JObject
    jo("danweiname") = Ary(0)
    jo("name") =  Ary(1)
    jo("date") = New JArray\'
    
    For Each dr As DataRow In DataTables("josn").Select("danweiname= \'" & Ary(0) & "\' and name=\'" & Ary(1) & "\'")
        Output.Show(dr("tiem"))
        Output.Show(dr("context"))
        jo2 = New JObject
        jo2("tiem") = dr("tiem")
        jo2("context") = dr("context")
        jo("date").add(jo2)
    Next
    jaaa.Add(jo)
Next

Output.Show(jaaa.ToString)
[此贴子已经被作者于2020/7/20 17:13:28编辑过]

--  作者:李孝春
--  发布时间:2020/7/20 17:35:00
--  回复:(有点蓝)Dim Arys As List(Of String())Arys ...
再麻烦老师指导下  上述代码运行还是没有成功 提示如下:
jo("date").add(jo2)
这个提示没有ADD成员  我tab查看  精灵提示里面只有parse tostring两个 

--  作者:有点蓝
--  发布时间:2020/7/21 8:20:00
--  
For Each Ary As String() In Arys
    Output.Show(Ary(0) & "|" & Ary(1))
    jo = New JObject
    jo("danweiname") = Ary(0)
    jo("name") =  Ary(1)
    dim ja2 as New JArray\'
    
    For Each dr As DataRow In DataTables("josn").Select("danweiname= \'" & Ary(0) & "\' and name=\'" & Ary(1) & "\'")
        Output.Show(dr("tiem"))
        Output.Show(dr("context"))
        jo2 = New JObject
        jo2("tiem") = dr("tiem")
        jo2("context") = dr("context")
        ja2.add(jo2)
    Next
    jo("date") = ja2
    jaaa.Add(jo)
Next

--  作者:李孝春
--  发布时间:2020/7/21 9:14:00
--  
老师  还是有点小问题 没有能够成功生成想要的josn 麻烦老师指导下
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:josn测试.foxdb



代码内容
Dim Arys As List(Of String())
Arys = DataTables("josn").GetValues("danweiname|name") 
Dim jaaa As New JArray
Dim jo,jo2 As JObject
For Each Ary As String() In Arys
    Output.Show(Ary(0) & "|" & Ary(1))
    jo = New JObject
    jo("danweiname") = Ary(0)
    jo("name") =  Ary(1)
    Dim ja2 As New JArray\'
    
    For Each dr As DataRow In DataTables("josn").Select("danweiname= \'" & Ary(0) & "\' and name=\'" & Ary(1) & "\'")
        Output.Show(dr("tiem"))
        Output.Show(dr("context"))
        jo2 = New JObject
        jo2("tiem") = dr("tiem")
        jo2("context") = dr("context")
        ja2.add(jo2)
    Next
    jo("date") = ja2
    jaaa.Add(jo)
Next

运行提示:

图片点击可在新窗口打开查看此主题相关图片如下:11111.jpg
图片点击可在新窗口打开查看
[此贴子已经被作者于2020/7/21 9:17:15编辑过]

--  作者:有点蓝
--  发布时间:2020/7/21 9:41:00
--  
jo2("tiem") = cstr(dr("tiem"))
--  作者:李孝春
--  发布时间:2020/7/21 17:47:00
--  回复:(有点蓝)jo2("tiem") = cstr(dr("tiem"))
有点蓝老师  经过你的指导 现在代码如下:
Dim Arys As List(Of String())
Arys = DataTables("josn").GetValues("danweiname|name")
Dim jaaa As New JArray
Dim jo,jo2 As JObject
For Each Ary As String() In Arys
    \'Output.Show(Ary(0) & "|" & Ary(1))
    jo = New JObject
    jo("danweiname") = Ary(0)
    jo("name") =  Ary(1)
    Dim ja2 As New JArray\'
    For Each dr As DataRow In DataTables("josn").Select("danweiname= \'" & Ary(0) & "\' and name=\'" & Ary(1) & "\'")
        \'Output.Show(dr("tiem"))
        \'Output.Show(dr("context"))
        jo2 = New JObject
        jo2("tiem") = cstr(dr("tiem"))
        jo2("context") = cstr(dr("context"))
        ja2.add(jo2)
    Next
    jo("date") = ja2
    jaaa.Add(jo)
Next
\'Output.Show(jaaa.ToString)
Dim content As String
content &= jaaa.ToString
\'Output.Show(content)
Dim lj As String = Date.Today & "save.json"
Dim FileName As String = ProjectPath & lj
FileSys.WriteAllText(FileName, content, True)

代码运行后得到的json达到预期,并且成功将json内容写入文件里
[
  {
    "danweiname": "AAAA",
    "name": "BB",
    "date": [
      {
        "tiem": "2020-09-01",
        "context": "CES"
      },
      {
        "tiem": "2020-11-11",
        "context": "REW"
      },
      {
        "tiem": "2020-12-12",
        "context": "TRE"
      }
    ]
  },
  {
    "danweiname": "AAAA",
    "name": "QQ",
    "date": [
      {
        "tiem": "2019-12-12",
        "context": "UY"
      },
      {
        "tiem": "2020-11-21",
        "context": "IUYIU"
      }
    ]
  },
  {
    "danweiname": "BBBB",
    "name": "CC",
    "date": [
      {
        "tiem": "2019-03-20",
        "context": "RTYE"
      },
      {
        "tiem": "2020-09-03",
        "context": "EWR"
      }
    ]
  },
  {
    "danweiname": "CCCC",
    "name": "WW",
    "date": [
      {
        "tiem": "2018-09-01",
        "context": "TREWR"
      },
      {
        "tiem": "2019-02-01",
        "context": "Q"
      }
    ]
  },
  {
    "danweiname": "DDDD",
    "name": "EE",
    "date": [
      {
        "tiem": "2020-07-21",
        "context": "432W"
      }
    ]
  }
]


参照帮助代码如下:
Dim json As String = jaaa.ToString
Dim jojo As JObject = JObject.Parse(json)
Output.Show("单位名称:" & jojo("danweiname"))
Output.Show("姓名:" & jojo("name"))
For Each ep As JToken In jojo("date")
    Output.Show( "日期:" & ep("tiem").ToString() &  "记录:" & ep("cotext").ToString()   )
Next
运行无法正常,提示报错,麻烦看看是不是我将json结构理解有误。
[此贴子已经被作者于2020/7/21 17:48:00编辑过]

--  作者:李孝春
--  发布时间:2020/7/21 18:00:00
--  回复:(有点蓝)jo2("tiem") = cstr(dr("tiem"))
有点蓝老师 参照你发布在论坛里的exweui我试一下将所生成的json文件解析成如下页面  没有成功  麻烦老师指导下  谢谢

图片点击可在新窗口打开查看此主题相关图片如下:11111.jpg
图片点击可在新窗口打开查看

以下内容是专门发给李孝春浏览



内部函数名json  
内容:
Dim e As RequestEventArgs = args(0)
Dim wb As New weui
wb.AppendHTML("<link rel=\'stylesheet\' href=\'./weui/exweui.css\'/>",True)
Dim fl As String = HttpServer.WebPath & "\\save.json"
If FileSys.FileExists(fl)
    Dim txt As String = FileSys.ReadAllText(fl)
    Dim jo As JObject = Jobject.Parse(txt)
    \'If jo("danweiname") IsNot Nothing Then
        With ExWeUI.WebUI.AddStepGroup("","step1","",,2)
            For Each jt As JToken In jo("date")
                With .AddStep("st01","",jt("tiem").ToString)
                    .Description = jt("context").ToString
                End With
            Next
            wb.InsertHTML(.BuildHtml)
        End With
    \'End If
    e.WriteString(wb.Build)
Else
    e.WriteString("没有查到信息")
End If

运行后页面提示错误
.NET Framework 版本:4.0.30319.42000
Foxtable 版本:2020.5.29.8
错误所在事件:自定义函数,json
详细错误信息:
调用的目标发生了异常。
Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path \'\', line 1, position 1.