C# 将Application Insights API调用结果转换为通用字典未终止字符串。预期分隔符

C# 将Application Insights API调用结果转换为通用字典未终止字符串。预期分隔符,c#,.net,json,azure-application-insights,C#,.net,Json,Azure Application Insights,我正在使用Application Insight记录CustomEvent。我想通过代码(c#)查询它。我发现了如何使用,以及如何使用GET/Query运行查询并获得JSON结果。我正在尝试使用now转换数据,在从下面的代码将数据转换为json时,我面临以下问题。当我试图从API获取异常时,出现了这个问题。 我得到问题的行json2=JsonConvert.DeserializeObject(a.Trim())下面是我收到的错误: 未终止的字符串。预期分隔符:“.Path”[15]”,第1行,位

我正在使用Application Insight记录CustomEvent。我想通过代码(c#)查询它。我发现了如何使用,以及如何使用GET/Query运行查询并获得JSON结果。我正在尝试使用now转换数据,在从下面的代码将数据转换为json时,我面临以下问题。当我试图从API获取异常时,出现了这个问题。 我得到问题的行
json2=JsonConvert.DeserializeObject(a.Trim())下面是我收到的错误:

未终止的字符串。预期分隔符:“.Path”[15]”,第1行,位置2017。

static void Main(字符串[]args)
{
字符串URL=
"https://api.applicationinsights.io/v1/apps/{0}/{1}";
字符串apikey=“xxxxx”;
字符串appid=“xxxx”;
string query=“query?query=customEvents |其中timestamp>ago(30d)|按时间戳排列的前5名”;
字符串结果=”;
HttpClient=新的HttpClient();
client.DefaultRequestHeaders.Accept.Add(
新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
client.DefaultRequestHeaders.Add(“x-api-key”,apikey);
var req=string.Format(URL、appid、查询);
HttpResponseMessage response=client.GetAsync(req).Result;
if(响应。IsSuccessStatusCode)
{
结果=response.Content.ReadAsStringAsync().result;
}
其他的
{
结果=响应。原因短语;
}
//获取结果的模式,例如有多少列以及每列的名称
string schema=result.Remove(0,result.IndexOf(“\”columns\:”)+1);
schema=schema.Remove(schema.IndexOf(“]),Remove(0,schema.IndexOf(“[”));
schema=schema+“]”;
//定义用于存储结构化结果的字典
Dictionary dict=新字典();
//将模式字符串转换为json
var json=JsonConvert.DeserializeObject(模式);
foreach(json中的var项)
{
var t1=((JObject)项);
变量t2=((作业对象)项);
字符串s1=t1.ToString();
列表=新列表();
dict.Add(s1.Replace(“\”name\”:“,”).Trim(),list);
}
//将值添加到字典中
//格式化字符串
字符串new\u content=result.Remove(0,result.IndexOf(“\”行\“:[”)。替换(“\”行\“:[”,”)。替换(“]}]},”);
//将每行值添加到数组中
var row_array=new_content.Split(']');
foreach(行数组中的变量t)
{
//如果该行为空,则忽略它
如果(t.Length==0)继续;
整数计数=0;
字符串a=“”;
List json2=null;
如果(t.StartsWith(“,”))
{
a=t.Remove(0,1)+“]”;
json2=JsonConvert.DeserializeObject(a.Trim());
}
如果(!t.EndsWith(“]),则为else
{
a=t+“]”;
json2=JsonConvert.DeserializeObject(a.Trim());
}
foreach(json2中的var项)
{
var s2=((JValue)项).ToString();
dict[dict.Keys.ElementAt(count)].Add(s2);
计数++;
}
}
Console.WriteLine(“--done--”);
Console.ReadLine();
}

您可以尝试为结果创建一个类模型并反序列化到此模型中。然后您就不需要解析json了。我进一步研究了这个问题,这是因为我们在一个对象中有一个动态对象,该对象中有[]当代码试图使用//将每行值添加到数组var row_array=new_content.split(']')进行拆分时这是一个问题,如果你也考虑类模型,这里是一个工具,它将为你从JSON生成类,谢谢你的回复,但它不是生成类,我已经检查了JSON,它是来自APIVILD,请复制和粘贴堆栈跟踪,而不是张贴图像,如果你认为它是相关的。。有关详细信息,请参阅。您可以尝试为结果创建一个类模型,并反序列化到此模型中。这样您就不需要解析json。我进一步研究了这个问题,这是因为我们在一个对象中拥有的数据是动态对象,该对象具有[]当代码试图使用//将每行值添加到数组var row_array=new_content.split(']')进行拆分时这是一个问题,如果你也考虑类模型,这里是一个工具,它将为你从JSON生成类,谢谢你的回复,但它不是生成类,我已经检查了JSON,它是来自APIVILD,请复制和粘贴堆栈跟踪,而不是张贴图像,如果你认为它是相关的。。有关详细信息,请参阅。
static void Main(string[] args)
    {

        string URL =
    "https://api.applicationinsights.io/v1/apps/{0}/{1}";

        string apikey = "xxxxx";
        string appid = "xxxx";
        string query = "query?query=customEvents| where timestamp >ago(30d)| top 5 by timestamp";

        string result = "";

        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Add("x-api-key", apikey);

        var req = string.Format(URL, appid, query);

        HttpResponseMessage response = client.GetAsync(req).Result;
        if (response.IsSuccessStatusCode)
        {
            result = response.Content.ReadAsStringAsync().Result;
        }
        else
        {
            result = response.ReasonPhrase;
        }

        //get the schema of the results, like how many columns and each columns' name
        string schema = result.Remove(0, result.IndexOf("\"columns\":") + 1);
        schema = schema.Remove(schema.IndexOf("],")).Remove(0, schema.IndexOf("["));
        schema = schema + "]";

        //define a dictionary for storing structured results
        Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();

        //convert schema string to json
        var json = JsonConvert.DeserializeObject<List<dynamic>>(schema);

        foreach (var item in json)
        {
            var t1 = ((JObject)item).First;
            var t2 = ((JObject)item).Last;

            string s1 = t1.ToString();

            List<string> list = new List<string>();
            dict.Add(s1.Replace("\"name\":", "").Trim(), list);
        }


        //add the value to the dictionary
        //format the string
        string new_content = result.Remove(0, result.IndexOf("\"rows\":[")).Replace("\"rows\":[", "").Replace("]}]}", "");

        //add each row of value to an array
        var row_array = new_content.Split(']');

        foreach (var t in row_array)
        {
            //if the row is empty, ignore it
            if (t.Length == 0) continue;

            int count = 0;
            string a = "";
            List<dynamic> json2 = null;

            if (t.StartsWith(","))
            {
                a = t.Remove(0, 1) + "]";
                json2 = JsonConvert.DeserializeObject<List<dynamic>>(a.Trim());
            }
            else if (!t.EndsWith("]"))
            {
                a = t + "]";

                json2 = JsonConvert.DeserializeObject<List<dynamic>>(a.Trim());
            }

            foreach (var item in json2)
            {
                var s2 = ((JValue)item).ToString();
                dict[dict.Keys.ElementAt(count)].Add(s2);
                count++;
            }
        }

        Console.WriteLine("---done---");
        Console.ReadLine();
    }