C#json反序列化对象

时间:2015-05-20 23:11:21

标签: c# json

我是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; }
}

一切都很好,除了“ActionDetails”中的“customVariables”,它只是将其设置为object,其中一个值为string:

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

它根本没有反序列化。

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

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>
        }
      }
   }
}

3 个答案:

答案 0 :(得分:5)

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

因此,我们尝试通过更改customVariables成员变量的类型声明并检查其变形,将其转换为更好地类似于输入JSON结构的形状以及反序列化结果的特定用法,分两步执行:每次更改后反序列化的内容。

  1. 将其设为词典

    public Dictionary<string, object> customVariables { get; set; }
    

    这将导致字典包含具有键"1"的单个元素和单个字符串值:

    {
        "customVariablePageName1": "URL",
        "customVariablePageValue1": "http://mysite.info/p"
    }
    
  2. 将其设为词典词典:

    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"]);
            }
        }
    }
    

    其类似于您的视图foreach,并将生成以下输出:

    Visitor: a393fed00271f588
      Action: action
        Custom variable #1
        Value: http://mysite.info/p
    

答案 1 :(得分:0)

由于我仍然无法发表评论,因此我必须将此作为答案发布。你尝试过使用C#的内置功能吗? See Here

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

另外,从它的外观来看,customVariables段似乎是一个数组或一个破碎的对象。我这样说,因为它缺少前面声明中的方括号,使它看起来像:

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

希望它有所帮助。

答案 2 :(得分:0)

试试这个结构

    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; }
}