以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 将新地址组合成新的值写入材料附件字段 提示System.InvalidCastException: 无法将类型为“System.String”的对象强制转换为类型“Newtonsoft.Json.Linq.JToken” (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=189167) |
-- 作者:cnsjroom -- 发布时间:2023/11/14 17:25:00 -- 将新地址组合成新的值写入材料附件字段 提示System.InvalidCastException: 无法将类型为“System.String”的对象强制转换为类型“Newtonsoft.Json.Linq.JToken” 材料附件值为: [{"url":"/Abspath/9b6a93cc-d27c-4e93-a4ba-c0092627274b/9b6a93cc-d27c-4e93-a4ba-c0092627274b.pdf", "name":"EventCode.pdf"}] 代码:实现文件数据的抽取后,将新地址组合成新的值写入材料附件字段 红色部分报错 Dim cmd As New SQLCommand cmd.Co nne ction Name = "主数据源" \'记得设置数据源名称 cmd.CommandText = "S el ect * From {评查活动案件卷宗材料}" Dim dt1 As DataTable = cmd.ExecuteReader Dim ja1 As New JArray Dim drs1 As List(Of DataRow) = dt1.Select("") For Each dr1 As DataRow In drs1 Dim json As String = dr1("材料附件").ToString Dim jsonObjects As JArray = JArray.Parse(json) Dim firstJsonObject As JObject = jsonObjects(0) Dim urlValue As String = firstJsonObject("url") Dim Values() As String Values = urlValue.split("/") For Index As Integer = 0 To Values.Length - 1 \' If FileSys.DirectoryExists("D:\\12+3\\myfile\\" & Values(2)) Then \' If FileSys.FileExists("D: \\ 12 + 3 \\ myfile \\ " & Values(2) & " \\ " & Values(3)) Then \' Else \' Output.Show("D:\\12+3\\myfile\\" & Values(2) & "\\" & Values(3)) \' If FileSys.DirectoryExists("C:\\MyFolder1\\" & dr1("活动id") & "\\" & dr1("案件id") & "\\" & Values(2)) Then \'如果目录C:\\MyFolder存在 \' If FileSys.FileExists("C:\\MyFolder1\\" & dr1("活动id") & "\\" & dr1("案件id") & "\\" & Values(2) & "\\" & Values(3)) Then \' output.Show( Values(3) & "文件已经存在!") \' Else \' output.Show( Values(3) & "文件不存在或删除!") \' FileSys.CopyFile("D:\\12+3\\myfile\\" & Values(2) & "\\" & Values(3), "C:\\MyFolder1\\" & dr1("活动id") & "\\" & dr1("案件id") & "\\" & Values(2) & "\\" & Values(3), True) \' End If \' Else \' FileSys.CreateDirectory("C:\\MyFolder1\\" & dr1("活动id") & "\\" & dr1("案件id")) \' If FileSys.FileExists("C:\\MyFolder1\\" & dr1("活动id") & "" & dr1("案件id") & "\\" & Values(2) & "\\" & Values(3)) Then \' output.Show( Values(3) & "文件已经存在!") \' Else \' output.Show( Values(3) & "文件不存在或删除!") \' FileSys.CopyFile("D:\\12+3\\myfile\\" & Values(2) & "\\" & Values(3), "C:\\MyFolder1\\" & dr1("活动id") & "\\" & dr1("案件id") & "\\" & Values(2) & "\\" & Values(3), True) \' End If \' End If Dim dt As DataTable = cmd.ExecuteReader Dim drs As List(Of DataRow) = dt1.Select("") Dim j As New JObject Dim ja As New JArray For Each dr As DataRow In drs Dim jo As New JObject jo("url") = "C:\\MyFolder1\\" & dr1("活动id") & "\\" & dr1("案件id") & "\\" & Values(2) & "\\" jo("name") = Values(3) ja.Add(jo) Output.show(ja.ToString) Next \' End if \' Else \' End If Next Next 运行提示: --------------------------- 版本:2022.8.18.1 --------------------------- 代码执行出错,错误信息: System.InvalidCastException: 无法将类型为“System.String”的对象强制转换为类型“Newtonsoft.Json.Linq.JToken”。 在 UserCode.Test() --------------------------- 确定 --------------------------- |
-- 作者:有点蓝 -- 发布时间:2023/11/14 17:27:00 -- Dim urlValue As String = firstJsonObject("url").tostring() |
-- 作者:cnsjroom -- 发布时间:2023/11/14 17:52:00 -- 回复:(有点蓝)Dim urlValue As String = firstJson... 老师 现在能够进行值的josn了 但是红色部分会循环四*四 也就是每条数据会有重复的十六条产生,有没有办法去重呢? Dim cmd As New SQLCommand cmd.Connecti \'记得设置数据源名称 cmd.Com mandText = "Se lect * From {评查活动案件卷宗材料}" Dim dt1 As DataTable = cmd.ExecuteReader Dim ja1 As New JArray Dim drs1 As List(Of DataRow) = dt1.Select("") For Each dr1 As DataRow In drs1 Dim json As String = dr1("材料附件").ToString Dim jsonObjects As JArray = JArray.Parse(json) Dim firstJsonObject As JObject = jsonObjects(0) Dim urlValue As String = firstJsonObject("url").tostring() Dim Values() As String Values = urlValue.split("/") For Index As Integer = 0 To Values.Length - 1 output.show(Values.Length) Output.Show(Values(0)) Output.Show(Values(1)) Output.Show(Values(2)) Output.Show(Values(3)) If Values(3) <> "" Then If FileSys.DirectoryExists("D:\\12+3\\myfile\\" & Values(2)) Then If FileSys.FileExists("D: \\ 12 + 3 \\ myfile \\ " & Values(2) & " \\ " & Values(3)) Then Else If FileSys.DirectoryExists("C:\\MyFolder1\\" & dr1("活动id") & "\\" & dr1("案件id") & "\\" & Values(2)) Then \'如果目录C:\\MyFolder存在 If FileSys.FileExists("C:\\MyFolder1\\" & dr1("活动id") & "\\" & dr1("案件id") & "\\" & Values(2) & "\\" & Values(3)) Then Else output.Show( Values(3) & "文件不存在或删除!") FileSys.CopyFile("D:\\12+3\\myfile\\" & Values(2) & "\\" & Values(3), "C:\\MyFolder1\\" & dr1("活动id") & "\\" & dr1("案件id") & "\\" & Values(2) & "\\" & Values(3), True) End If Else FileSys.CreateDirectory("C:\\MyFolder1\\" & dr1("活动id") & "\\" & dr1("案件id")) If FileSys.FileExists("C:\\MyFolder1\\" & dr1("活动id") & "" & dr1("案件id") & "\\" & Values(2) & "\\" & Values(3)) Then Else output.Show( Values(3) & "文件不存在或删除!") FileSys.CopyFile("D:\\12+3\\myfile\\" & Values(2) & "\\" & Values(3), "C:\\MyFolder1\\" & dr1("活动id") & "\\" & dr1("案件id") & "\\" & Values(2) & "\\" & Values(3), True) End If End If Dim j As New JObject Dim ja As New JArray Dim jo As New JObject Dim ss As String = "C:\\MyFolder1\\" & dr1("活动id") & "\\" & dr1("案件id") & "\\" & Values(3) SS = SS.Replace("\\", "/") jo("url") = ss jo("name") = Values(3) ja.Add(jo) Output.show(ja.ToString) End If Else End If End If Next Next |
-- 作者:有点蓝 -- 发布时间:2023/11/14 20:11:00 -- 既然循环多了,那就去掉一个循环啊,第2个for循环的目的是什么? |