Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# json deserialize object

Tags:

json

c#

I am new with C#, and so I don't know too much about it. I want to deserialize json object, but I am having some issues.

Thi is json object:

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:

{
 "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>
        }
      }
   }
}
like image 677
carpics Avatar asked Jan 31 '26 23:01

carpics


1 Answers

Well, this happens, because you have specified that the customVariables member is of type 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.

  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:

    {
        "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:

    Visitor: a393fed00271f588
      Action: action
        Custom variable #1
        Value: http://mysite.info/p
    
like image 144
Alex Avatar answered Feb 03 '26 12:02

Alex