简体   繁体   English

C#json反序列化对象

[英]C# json deserialize object

I am new with C#, and so I don't know too much about it. 我是C#的新手,所以我不太了解它。 I want to deserialize json object, but I am having some issues. 我想反序列化json对象,但我遇到了一些问题。

Thi is json object: 这是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",
   }] 

}]";

And I am trying to deserialize it on this way: 我试图以这种方式反序列化它:

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

Everything is fine, except "customVariables" in "ActionDetails" it just set it to object with one value as string: 一切都很好,除了“ActionDetails”中的“customVariables”,它只是将其设置为object,其中一个值为string:

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

It doesn't deserialize it at all. 它根本没有反序列化。

I need this deserialize so I can say: 我需要这个反序列化,所以我可以说:

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

Well, this happens, because you have specified that the customVariables member is of type System.Object . 嗯,发生这种情况,因为您已指定customVariables成员的类型为System.Object So deserialization will result in assigning it the string value. 因此反序列化将导致为其分配字符串值。

So lets try to mold it into a shape that better resembles the input JSON structure and your specific use of the deserialization result, in two steps, by changing the type declaration of the customVariables member variable, and inspecting its deserialized content after each change. 因此,我们尝试通过更改customVariables成员变量的类型声明,并在每次更改后检查其反序列化内容,尝试将其塑造成更好地类似于输入JSON结构的形状以及反序列化结果的特定用法。

  1. Make it a dictionary : 把它作为字典

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

    This will result in a dictionary that contains a single element with the key "1" and a single string value: 这将导致包含单个元素的字典,其中键为"1"并且包含单个字符串值:

     { "customVariablePageName1": "URL", "customVariablePageValue1": "http://mysite.info/p" } 
  2. Make it a dictionary of dictionaries: 使它成为字典词典:

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

    And print its deserialized ouput like this: 并打印出这样的反序列化输出:

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

    Which resembles your view's foreach , and will produce the following output: 这类似于您的视图的foreach ,并将产生以下输出:

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

Since I can't still comment I will have to post this as an answer. 由于我不能发表评论,我将不得不将此作为答案发布。 have you tried using the built in function of C#? 你尝试过使用C#的内置功能吗? See Here 看这里

Also, from what I gather, on the Documentation of that method it converts it converts the json into a system object. 另外,根据我收集的内容,在该方法的文档中,它转换它将json转换为系统对象。

Also, from the looks of it it seems like the customVariables segment is either an array or a broken object. 此外,从它的外观来看,似乎customVariables段是一个数组或一个破碎的对象。 I say that because it is missing the square brackets like in the previous declarations, making it look like: 我这样说是因为它缺少前面声明中的方括号,使它看起来像:

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

Hope it helps. 希望能帮助到你。

try this structure 试试这个结构

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM