C # json deserialize object

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

Thi is a 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 like this:

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 for the "customVariables" in "ActionDetails", it just set it to a single value object as a string:

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

      

It doesn't deserialize it at all.

I need this deserializer, 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>
        }
      }
   }
}

      

+3


source to share


3 answers


Well, this is happening because you specified that the member customVariables

is of type System.Object

. So deserialization will result in a string value being assigned to it.

So let's try to turn it into a form 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 member variable customVariables

and checking its deserialized content after each change.



  • Make a dictionary :

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

    This will result in a dictionary containing one item with a key "1"

    and one string value:

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

  • Make a dictionary of dictionaries:

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

    And print its deserialized output 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 is similar to your view foreach

    , and produces the following output:

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

+5


source


Since I cannot comment, I will have to post this as an answer. have you tried using the built-in C # function? See here

Also, from what I collect, in the Documentation of this method, it converts it, it converts the json to a system object.

Also, it looks like the segment customVariables

is either an array or a broken object. I say that since there are no square brackets in the square brackets as in the previous declarations, it looks like this:



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

      

Hope it helps.

0


source


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

      

0


source







All Articles