Skip to content
Loading
  • +2

    Your last solution takes really long to process. I just did this below and it works fast perfect.

    List DetailList = new List();

    string[] splitJsonData = jsonData.Split("SendAction").Where(x => !string.IsNullOrEmpty(x)).ToArray();
    foreach (var item in splitJsonData )
    {
    string split = item.Substring(item.IndexOf('{'));
    DetailRoot seraphimDetail = JsonConvert.DeserializeObject(split);
    DetailList.Add(seraphimDetail);
    }

    +2
  • Muhammad Imran Ansari
    +1
    Use the following classes to deserialize your json:
    1. public class RootElement  
    2.     {  
    3.         public DateTime EventDate { get; set; }  
    4.         public string HeaderVersion { get; set; }  
    5.         public List Details { get; set; }  
    6.         public string Session { get; set; }  
    7.         public string SiteGuid { get; set; }  
    8.         public string UserEmail { get; set; }  
    9.         public string UserGuid { get; set; }  
    10.         public string UserID { get; set; }  
    11.     }  
    12.     public class Detail  
    13.     {  
    14.         public string ApplicationGuid { get; set; }  
    15.         public string ApplicationVersion { get; set; }  
    16.         public string ClickItemID { get; set; }  
    17.         public string Description { get; set; }  
    18.         public int Discount { get; set; }  
    19.         public string EventType { get; set; }  
    20.         public string ItemID { get; set; }  
    21.         public string ProductID { get; set; }  
    22.         public string ProductName { get; set; }  
    23.         public double ProductPrice { get; set; }  
    24.         public string Promo { get; set; }  
    25.         public int Quantity { get; set; }  
    26.         public int TransAmount { get; set; }  
    27.     }  
    1. // Place you Json into file and Load here  
    2. string yourJson = File.ReadAllText(@"E:\myJson.json");  
    3. var finalData = JsonConvert.DeserializeObject(yourJson);  
     In FinalData you will get the objects of classes.
    +1
  • +1
    This did not help. I am getting and error Because the string below is not in the format. The send action and datatime and cannot be parse SendAction 2018-10-18T20:04:01.000000 {
    +1
  • +1
    This did not help. I am getting and error Because the string below is not in the format. The send action and datatime and cannot be parse SendAction 2018-10-18T20:04:01.000000 {
    +1
  • +1

    public class Detail
    {
    public string ApplicationGuid { get; set; }
    public string ApplicationVersion { get; set; }
    public string ClickItemID { get; set; }
    public string Description { get; set; }
    public double Discount { get; set; }
    public string EventType { get; set; }
    public string ItemID { get; set; }
    public string ProductID { get; set; }
    public string ProductName { get; set; }
    public double ProductPrice { get; set; }
    public string Promo { get; set; }
    public int Quantity { get; set; }
    public int TransAmount { get; set; }
    }
    public class Root
    {
    public DateTime EventDate { get; set; }
    public string HeaderVersion { get; set; }
    public List Details { get; set; }
    public string Session { get; set; }
    public string SiteGuid { get; set; }
    public string UserEmail { get; set; }
    public string UserGuid { get; set; }
    public string UserID { get; set; }
    }

    then use 

    var myDeserializedClass = System.Text.Json.JsonSerializer.DeserializeObject(myJsonResponse);

    +1
  • Uttam Kumar
    +1

    Can you check the json format of the the data you have received first?
    The format does not look correct..

    +1
  • +1
    UTTAM everyone keeps sending me the class structure, i already have the class structure. The issue is that everyone is not looking the root of the structure, how do i get pass the sendaction and the datetime of the structure or outside th3 bracket without looping line by line or possibly adding the SendAction and Datetime to the structure. Keep in mind I cannot change the incoming data file.
    +1
  • Uttam Kumar
    +1
      
    If json stucture is valid, it'll be easier to deserialize. Otherwise you may need to apply unnecessary logic reading line by line.
     
    I have attached correct json format in the attachment.
     
    After receiving the response data, can you try to matching the format from your end by writing a small logic can help you fix this issue.
     
    Let me know if you need help in writing the logic.
    +1
  • +1

    Uttam, I need help with writing logic. There will be many records in one file that have many sendactions. So we have to make sure logic accounts for all sendactions

    +1
  • +1

    Also if we can ignore the SendAction and Datetime, Everthing works smooth that way also just so you

    +1
  • Uttam Kumar
    +1
    Can you attach a smaple file which have multiple sendactions for reference to write the logic?
    +1
  • +1

    Uttam, Will this work for and entire file of send actions?

    +1
  • +1

    Uttam review attachment and review logic. I sent  screenshot of whats going on in the debugger. Your logic is not accounting for all of the sendactions in the file. 
     
    public Dictionary> GetSiteData(List filenames)
    {
    if (filenames == null)
    throw new ArgumentNullException("filenames is null...");
    if (filenames.Count() == 0)
    throw new ArgumentOutOfRangeException("filenames count is out of range...");
    Dictionary> SiteDictionary = new Dictionary>();
    foreach (string f in filenames)
    {
    string json = File.ReadAllText(f);
    string result = json.Substring(json.IndexOf('{') + 1);
    var text = JsonConvert.DeserializeObject< List < SeraphimDetailRoot >> (json);
    }
    return null;
    }

    +1
  • +1
    Uttam, Here is a sample send action file
    +1
  • +1

    So the solution you provided works now, now I get an error that says "Additional Text encountered after finished reading JSON content when I try to execute the DeserializeObject below". 

    string json = File.ReadAllText(f);
    string[] splitJsonData = json.Split("SendAction").Where(x => !string.IsNullOrEmpty(x)).ToArray();
    newJsonData = new StringBuilder();
    foreach (var item in splitJsonData)
    {
    newJsonData.Append(item.Substring(item.IndexOf('{')));
    }
    var text = newJsonData.ToString();
     
    DetailRoot output = JsonConvert.DeserializeObject(text); 

    +1
  • +1
    Uttam can you look at the additional text encounter error with your logic after finish reading  json content
    +1
  • Uttam Kumar
    +1
    Sorry, for late reply.
     
    I have changed the logic slightly to make correct json format.
     
    You need to add a class which will have property of type List<DetailRoot> .   
     
    1. public class SendActionData  
    2. {  
    3.     public List SendAction { get; set; }  
    4. }  
     
    1. string jsonData = "SendAction 2018-10-18T20:04:01.000000 { \"EventDate\": \"2018-10-18T20:04:01.8278435\" } SendAction 2018-10-18T20:04:01.000000 { \"EventDate\": \"2018-10-18T20:04:01.8278435\" }";  
    2. string[] splitJsonData = jsonData.Split("SendAction").Where(x => !string.IsNullOrEmpty(x)).ToArray();  
    3. string newJsonData = "";  
    4. foreach (var item in splitJsonData)  
    5. {  
    6.     newJsonData += item.Substring(item.IndexOf('{'));  
    7.     newJsonData += ",";  
    8. }  
    9. string newValidJsonData = newJsonData.TrimEnd(',');  
    10. newValidJsonData = "{\"SendAction\":[" + newValidJsonData + "]}";  
    11.   
    12. SendActionData output = JsonConvert.DeserializeObject(newValidJsonData);  
     
     Please try checking this code. I hope this should work fine. :)
    +1
  • Uttam Kumar
    0

    You can substring SendAction and Datetime if there is only one occurance. For multiple SendAction we may need to write different logic

    C#
    string jsonData = "SendAction 2018-10-18T20:04:01.000000 { \"EventDate\"";  
    string result = jsonData.Substring(jsonData.IndexOf('{'));
    0