这是我要在这里实现的目标:
我遇到的问题是尝试使用我收集的结果中的文件名。
运行代码时,会显示从数据库收集的原始JSON数据,但是当尝试仅列出文件名时,我什么也没得到(甚至没有错误)
关于如何解决此问题并以一种让我以后搜索它们的方式列出文件名的想法?还是有更好的方法来做到这一点?
另请注意:由于使用的是SQL Server版本,因此我必须使用FOR XML,因为FOR JSON不兼容。
编辑:使用Prany提供的代码,我现在现在只能输出音频文件名,但是我尝试并也输出sCallId,但出现重复问题(请参见下面的输出):
Getting Calls 2016\03\30\300320161614210106361-00000934412405.asf--84377-3668343241-514513 2016\03\30\300320161614210106361-00000934412405.asf--84385-3668343557-255773 2016\03\30\300320161614210106361-00000934412405.asf--84392-3668344445-516453 2016\03\30\300320161614210106361-00000934412405.asf--85000-3668749568-733799 2016\03\30\300320161614210106361-00000934412405.asf--85604-3668872399-722313 2016\03\30\300320161620220106362-00000934052048.asf--84377-3668343241-514513 2016\03\30\300320161620220106362-00000934052048.asf--84385-3668343557-255773 2016\03\30\300320161620220106362-00000934052048.asf--84392-3668344445-516453 2016\03\30\300320161620220106362-00000934052048.asf--85000-3668749568-733799 2016\03\30\300320161620220106362-00000934052048.asf--85604-3668872399-722313 2016\03\30\300320161634220106363-00000933211384.asf--84377-3668343241-514513 2016\03\30\300320161634220106363-00000933211384.asf--84385-3668343557-255773 2016\03\30\300320161634220106363-00000933211384.asf--84392-3668344445-516453 2016\03\30\300320161634220106363-00000933211384.asf--85000-3668749568-733799 2016\03\30\300320161634220106363-00000933211384.asf--85604-3668872399-722313 2016\04\04\040420160908190106389-00000527974488.asf--84377-3668343241-514513 2016\04\04\040420160908190106389-00000527974488.asf--84385-3668343557-255773 2016\04\04\040420160908190106389-00000527974488.asf--84392-3668344445-516453 2016\04\04\040420160908190106389-00000527974488.asf--85000-3668749568-733799 2016\04\04\040420160908190106389-00000527974488.asf--85604-3668872399-722313 2016\04\05\050420161913220106406-00000405271715.asf--84377-3668343241-514513 2016\04\05\050420161913220106406-00000405271715.asf--84385-3668343557-255773 2016\04\05\050420161913220106406-00000405271715.asf--84392-3668344445-516453 2016\04\05\050420161913220106406-00000405271715.asf--85000-3668749568-733799 2016\04\05\050420161913220106406-00000405271715.asf--85604-3668872399-722313
下面是我目前正在尝试使用的代码。
//Run the SQL and wrap the output in results tags to fix Multiple Root Elements error. string liveXML = "<results>" + cmd2.ExecuteScalar().ToString() + "</results>"; //Create new XML Document XmlDocument LiveDoc = new XmlDocument(); LiveDoc.LoadXml(liveXML); //Conver XML to JSON sjsonLive = JsonConvert.SerializeXmlNode(LiveDoc); //Output RAW JSON txtOut.AppendText("\r\n" + sjsonLive); //Parse JSON into an Array var files = JObject.Parse(sjsonLive); //We want to run this values in a files seach, but for now let's print it to txtOut foreach (var f in files.SelectTokens("$..calls..@audioFileName")) foreach (var c in files.SelectTokens("$..calls..@sCallID")) { txtOut.AppendText("\r\n" + f.ToString() + " - " + c.ToString()); //Conduct File Search Here... }
JSON数据示例:
{ "results": { "calls": [{ "@audioFileName": "2016\\03\\30\\300320161614210106361-00000934412405.asf", "@sCallID": "84377-3668343241-514513" }, { "@audioFileName": "2016\\03\\30\\300320161620220106362-00000934052048.asf", "@sCallID": "84385-3668343557-255773" }, { "@audioFileName": "2016\\03\\30\\300320161634220106363-00000933211384.asf", "@sCallID": "84392-3668344445-516453" }, { "@audioFileName": "2016\\04\\04\\040420160908190106389-00000527974488.asf", "@sCallID": "85000-3668749568-733799" }, { "@audioFileName": "2016\\04\\05\\050420161913220106406-00000405271715.asf", "@sCallID": "85604-3668872399-722313" } ] } }
编辑:
您可以在下面使用令牌选择器
files.SelectTokens("$..calls..@audioFileName")
编辑2:
var calls = files.SelectTokens("$..calls..@sCallID").ToList(); var audiofiles = files.SelectTokens("$..calls..@audioFileName").ToList(); for (int i = 0; i <= audiofiles.Count; i++) { //Conduct File search if (true) { //access by index like audiofiles[i] and append to the query } else { //access calls by index like calls[i] and append to the query } }