deserialize mongodb datetime field "$time" of JSON document field in C#/SSIS Script component

huangapple go评论63阅读模式
英文:

deserialize mongodb datetime field "$time" of JSON document field in C#/SSIS Script component

问题

I am trying to deserialize a mongodb's datetime "$time" field from a JSON file in Script Component SSIS/C# WITHOUT using newtonsoft. This is how far I have reached. Thanks for any help.

Example of my JSON file:

[
{
"studentId": A2336,
"LastUpdatedDateTime": { "$date": "2022-08-12T20:11:30.324Z" }
},
{
"studentId": B1470,
"LastUpdatedDateTime": { "$date": "2021-03-02T21:22:44.310Z" }
}
]

//current code
public override void CreateNewOutputRows() {
    string jsonFileContent = File.ReadAllText(@"C:\desktop\sample.json");
    JavaScriptSerializer js = new JavaScriptSerializer();
    List<Student> allStudents = js.Deserialize<List<Student>>(jsonFileContent);
    foreach (var student in allStudents) {
        Output0Buffer.AddRow();
        System.Windows.Forms.MessageBox.Show("Student Id is: " + student.studentId.ToString());
        Output0Buffer.StudentId = student.studentId;

        //how to print/convert the LastUpdatedDateTime to the datetime Output0Buffer field?
        //this gives an error that says "Object reference not set to an instance of an object.
        System.Windows.Forms.MessageBox.Show("Student Id is: " + student.LastUpdatedDateTime.date.ToString());
        //this yields in "Student Id is: "
        System.Windows.Forms.MessageBox.Show("Student Id is: " + student.LastUpdatedDateTime.date);

        //here I am trying to assign it to an output0buffer field
        Output0Buffer.LastUpdatedDateTime = student.LastUpdatedDateTime.date;
    }
}

public class Student {
    public string studentId { get; set; }
    public Jsondatetime LastUpdatedDateTime { get; set; }
}

public class Jsondatetime {
    public string date { get; set; }
}

I have tried creating a different class for the "$date" like shown in the code above. It yields an error when trying to print as a string and yields as blank when trying to print as is. I expected the exact value of the key "$date".

英文:

I am trying to deserialize a mongodb's datetime "$time" field from a JSON file in Script Component SSIS/C# WITHOUT using newtonsoft. This is how far I have reached. Thanks for any help.

Example of my JSON file:

[
{
"studentId":A2336,
"LastUpdatedDateTime":{ "$date":"2022-08-12T20:11:30.324Z"}} ,
{
"studentId":B1470,
"LastUpdatedDateTime":{ "$date":"2021-03-02T21:22:44.310Z"}
}
]

//current code
Public overridevoidCreateNewOutputRows(){
      String jsonFileContent = File.ReadAllText(@&quot;C:\desktop\sample.json&quot;);
      javaScriptSerializer js = newjavaScriptSerializer();
      List&lt;Student&gt; allStudents = js.Deserialize&lt;List&lt;Student&gt;&gt;(jsonFileContent);
foreach(var student inallStudents)
          {
               Output0Buffer.AddRow();
               System.Windows.Forms.MessageBox.Show(&quot;Student Id is: &quot;+ student.studentId.ToString());
               Output0Buffer.StudentId = student.studentId;
        
               //how to print/convert the LastUpdatedDateTime to the datetime Output0Buffer field?
              //this gives and error that says &quot;Object reference not set to an instance of an object.
               System.Windows.Forms.MessageBox.Show(&quot;Student Id is: &quot;+ student.LastUpdatedDateTime.date.ToString());
//this yields in &quot;Student Id is: &quot;
               System.Windows.Forms.MessageBox.Show(&quot;Student Id is: &quot;+ student.LastUpdatedDateTime.date);

             //here I am trying to assign it to an ouput0buffer field
                  Output0Buffer.LastUpdatedDateTime = student.LastUpdatedDateTime.date;

	}
}


Class Student
{
    Public String studentId { get; set;}
    Public Jsondatetime LastUpdatedDateTime  {get; set;}
}

Class Jsondatetime
{
    Public String date { get; set;}
}

I have tried creating a different class for the "$date" like shown in the code above. It yeilds to and error when trying to print as a string and yeilds as a blank when trying to print as is. I expected the exact value of the key "$date".

答案1

得分: 1

Your JSON is not valid; you need to fix it by adding double quotes to "studentId." Since you are using an outdated deserializer, try this code:

var jsonFileContent = "[ { \"studentId\":\"A2336\", \"LastUpdatedDateTime\":{\"$date\":\"2022-08-12T20:11:30.324Z\"}} , { \"studentId\":\"B1470\", \"LastUpdatedDateTime\":{\"$date\":\"2021-03-02T21:22:44.310Z\"} } ]";

jsonFileContent = jsonFileContent.Replace("\"$date\"", "\"date\");

List<Student> allStudents = js.Deserialize<List<Student>>(jsonFileContent);

DateTime date = allStudents[0].LastUpdatedDateTime.date;

public class Student
{
	public string studentId { get; set; }
	public LastUpdatedDateTime LastUpdatedDateTime { get; set; }
}
public class LastUpdatedDateTime
{
	public DateTime date { get; set; }
}
英文:

Your json is not valid, you have to fix it by adding " to studentId. Since you are using an ancient deserializer, try this code

var jsonFileContent = &quot;[ { \&quot;studentId\&quot;:\&quot;A2336\&quot;, \&quot;LastUpdatedDateTime\&quot;:{ \&quot;$date\&quot;:\&quot;2022-08-12T20:11:30.324Z\&quot;}} , { \&quot;studentId\&quot;:\&quot;B1470\&quot;, \&quot;LastUpdatedDateTime\&quot;:{ \&quot;$date\&quot;:\&quot;2021-03-02T21:22:44.310Z\&quot;} } ]&quot;;

jsonFileContent = jsonFileContent.Replace(&quot;\&quot;$date\&quot;&quot;, &quot;\&quot;date\&quot;&quot;);

 List&lt;Student&gt; allStudents = js.Deserialize&lt;List&lt;Student&gt;&gt;(jsonFileContent);

DateTime date = allStudents[0].LastUpdatedDateTime.date;


public class Student
{
	public string studentId { get; set; }
	public LastUpdatedDateTime LastUpdatedDateTime { get; set; }
}
public class LastUpdatedDateTime
{
	public DateTime date { get; set; }
}

</details>



huangapple
  • 本文由 发表于 2023年3月23日 12:38:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定