C#json反序列化对象

C#json反序列化对象,c#,json,C#,Json,我是C#的新手,所以我对C#不太了解。我想反序列化json对象,但我遇到了一些问题 这是json对象: var json = "[{ "idSite":"1", "visitorId":"a393fed00271f588", "actionDetails":[{ "type":"action", "url":"http:\/\/mysite.info\/test-24\/", "customVariables":{

我是C#的新手,所以我对C#不太了解。我想反序列化json对象,但我遇到了一些问题

这是json对象:

var json  = "[{

  "idSite":"1",
  "visitorId":"a393fed00271f588",
  "actionDetails":[{
     "type":"action",
      "url":"http:\/\/mysite.info\/test-24\/",
      "customVariables":{
                  "1":{
                       "customVariablePageName1":"URL",
                       "customVariablePageValue1":"http:\/\/mysite.info\/p"
                       }
                   },
      "timeSpent":"78",
   }] 

}]";
我正试图通过这种方式将其反序列化:

var visits = JsonConvert.DeserializeObject<VisitorDetails[]>(json);

public class VisitorDetails
{
    public string idSite { get; set; }

    public string visitorId { get; set; }

    public List<ActionDetail> actionDetails { get; set; }
}


public class ActionDetail
{
    public string type { get; set; }

    public string url { get; set; }

    public string timeSpent { get; set; }

    public object customVariables { get; set; }
}
它根本不反序列化它

我需要这个反序列化,这样我就可以说:

foreach (var visit in Model.PiwikInfo)
{
   @foreach (var action in visit.actionDetails)
   {
      @if (action.customVariables != null && action.customVariables.Any())
      {
        foreach (var cv in visit.customVariables.Where(cv => cv.HasProperty("customVariablePageName1")))
        {
         <span>URL: @cv.GetProperty("customVariablePageValue1")</span>
        }
      }
   }
}
foreach(Model.PiwikInfo中的var访问)
{
@foreach(访问中的var操作。操作详细信息)
{
@if(action.customVariables!=null&&action.customVariables.Any())
{
foreach(visit.customVariables.Where(cv=>cv.HasProperty(“customVariablePageName1”))中的变量cv)
{
URL:@cv.GetProperty(“customVariablePageValue1”)
}
}
}
}

因为我现在还不能发表评论,所以我不得不将此作为答案发布。你试过使用C的内置函数吗?

此外,根据我收集的信息,在该方法的文档中,它将json转换为系统对象

另外,从外观上看,
customVariables
段似乎是一个数组或一个断开的对象。我之所以这样说,是因为它缺少了前几次声明中的方括号,使它看起来像:

...
"customVariables":[{
                  "1":[{
                       "customVariablePageName1":"URL",
                       "customVariablePageValue1":"http:\/\/mysite.info\/p"
                       }]
                   }],
...

希望能有所帮助。

好吧,这种情况会发生,因为您已指定
customVariables
成员的类型为
System.Object
。因此,反序列化将导致为其分配字符串值

因此,让我们通过两个步骤,通过更改
customVariables
成员变量的类型声明,并在每次更改后检查其反序列化内容,尝试将其塑造成更类似于输入JSON结构和反序列化结果的特定用法的形状

  • 把它做成一本字典

    public Dictionary<string, object> customVariables { get; set; }
    
  • 让它成为一部词典:

    public Dictionary<string, Dictionary<string, string>> customVariables { get; set; }
    
  • 试试这个结构

        public class T1
    {
        public string customVariablePageName1 { get; set; }
        public string customVariablePageValue1 { get; set; }
    }
    
    public class CustomVariables
    {
        public T1 t1 { get; set; }
    }
    
    public class ActionDetail
    {
        public string type { get; set; }
        public string url { get; set; }
        public CustomVariables customVariables { get; set; }
        public string timeSpent { get; set; }
    }
    
    public class RootObject
    {
        public string idSite { get; set; }
        public string visitorId { get; set; }
        public List<ActionDetail> actionDetails { get; set; }
    }
    
    公共类T1
    {
    公共字符串customVariablePageName1{get;set;}
    公共字符串customVariablePageValue1{get;set;}
    }
    公共类自定义变量
    {
    公共T1 T1{get;set;}
    }
    公共类ActionDetail
    {
    公共字符串类型{get;set;}
    公共字符串url{get;set;}
    公共自定义变量自定义变量{get;set;}
    公共字符串timeSpent{get;set;}
    }
    公共类根对象
    {
    公共字符串idSite{get;set;}
    公共字符串访问ID{get;set;}
    公共列表actionDetails{get;set;}
    }
    
    我无法更改json,我从一个API获得响应。也没有尝试内置功能,因为出于某种原因,我需要以这种方式进行工作。
    public Dictionary<string, Dictionary<string, string>> customVariables { get; set; }
    
    var visits = JsonConvert.DeserializeObject<VisitorDetails[]>(json_string);
    
    foreach (var visit in visits)
    {
        Console.WriteLine("Visitor: {0}", visit.visitorId);
        foreach (var detail in visit.actionDetails)
        {
            Console.WriteLine("  Action: {0}", detail.type);
            foreach (var cv in detail.customVariables.Where(x => x.Value.ContainsKey("customVariablePageName1")))
            {
                Console.WriteLine("    Custom variable #{0}", cv.Key);
                Console.WriteLine("    Value: {0}", cv.Value["customVariablePageValue1"]);
            }
        }
    }
    
    Visitor: a393fed00271f588
      Action: action
        Custom variable #1
        Value: http://mysite.info/p
    
        public class T1
    {
        public string customVariablePageName1 { get; set; }
        public string customVariablePageValue1 { get; set; }
    }
    
    public class CustomVariables
    {
        public T1 t1 { get; set; }
    }
    
    public class ActionDetail
    {
        public string type { get; set; }
        public string url { get; set; }
        public CustomVariables customVariables { get; set; }
        public string timeSpent { get; set; }
    }
    
    public class RootObject
    {
        public string idSite { get; set; }
        public string visitorId { get; set; }
        public List<ActionDetail> actionDetails { get; set; }
    }