从C#中的JSON对象获取对象值 [英] Get Object Value from JSON Object in C#

查看:115
本文介绍了从C#中的JSON对象获取对象值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要在这里实现的目标:

Here is the what I am trying to achieve here:

  1. 从数据库中获取文件名和ID的列表
  2. 在网络路径上搜索这些文件
  3. 存储未找到文件的所有ID
  4. 在第二个数据库中搜索该ID
  5. 在网络路径上搜索这些文件
  6. 列出所有在两个位置均未找到文件的ID.

我遇到的问题是尝试使用我收集的结果中的文件名.

The issue I an encountering is trying to use the file names from results I have collected.

运行代码时,会显示从数据库收集的原始JSON数据,但是当尝试仅列出文件名时,我什么也没得到(甚至没有错误)

When running the code, the raw JSON data collected from the database gets displayed, however when trying to list just the file names I get nothing (not even an error)

关于如何解决此问题并列出文件名的任何想法,也可以让我以后搜索它们?还是有更好的方法来做到这一点?

Any ideas on how to fix this and list the file names in a way that would also let me search for them later? Or is there a better way to do this?

也请注意:由于使用的是SQL Server版本,我必须使用FOR XML,因为FOR JSON不兼容.

Also note: due to the version of SQL server being used i have to use FOR XML as FOR JSON is not compatible.

使用Prany提供的代码,我现在仅可以输出音频文件名,但是我尝试并也输出sCallId,但出现重复问题(请参见下面的输出):

Using code provided by Prany I am now able to output just the Audio File Name, but it I try and also output the sCallId I get a duplication Issue (see below output):

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

下面是我当前正在尝试使用的代码.

Below Is the code I am currently using to try and do this.

    //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"
        }
    ]
}
}

推荐答案

您可以在下面的令牌选择器中使用

You can use below token selector

files.SelectTokens("$..calls..@audioFileName")

 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

            }

        }

这篇关于从C#中的JSON对象获取对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆