Tostisto
Tostisto
CC#
Created by Tostisto on 7/9/2024 in #help
Problem with json deserialization
I have an issue with deserialization of the datetime from a JSON string. I need to deserialize 2024-07-09T10:36:23:000Z into a datetime object, but currently, I am having a problem with deserialization. My code looks like this:
using System;
using System.Text.Json;

string jsonString = "{\"Date\":\"2024-07-09T10:36:23:000Z\",\"Message\":\"Hello, World!\"}";

MyData? data = JsonSerializer.Deserialize<MyData>(jsonString);

Console.WriteLine(data.Date); // Output: 7/9/2024 10:36:23 AM

public class MyData
{
public DateTime Date { get; set; }
public string Message { get; set; }
}
using System;
using System.Text.Json;

string jsonString = "{\"Date\":\"2024-07-09T10:36:23:000Z\",\"Message\":\"Hello, World!\"}";

MyData? data = JsonSerializer.Deserialize<MyData>(jsonString);

Console.WriteLine(data.Date); // Output: 7/9/2024 10:36:23 AM

public class MyData
{
public DateTime Date { get; set; }
public string Message { get; set; }
}
With this error
System.Text.Json.JsonException: The JSON value could not be converted to System.DateTime. Path: $.Date | LineNumber: 0 | BytePositionInLine: 34.
---> System.FormatException: The JSON value is not in a supported DateTime format.
System.Text.Json.JsonException: The JSON value could not be converted to System.DateTime. Path: $.Date | LineNumber: 0 | BytePositionInLine: 34.
---> System.FormatException: The JSON value is not in a supported DateTime format.
Shoud be problem in culture setting in os? Or how can I serialize it without creating a new converter?
7 replies
CC#
Created by Tostisto on 7/9/2023 in #help
❔ Laptop Battery Info
I would like to create a simple app that monitors information about the battery, such as its overall health, capacity, voltage, and other relevant details. Currently, I have create a basic console app in .NET Core that retrieves some information about the battery. Here's the existing code:
using System;
using System.Management;

class Program
{
static void Main()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Battery");

foreach (ManagementObject queryObj in searcher.Get())
{
PropertyDataCollection properties = queryObj.Properties;

foreach (PropertyData property in properties)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
}
}
}
using System;
using System.Management;

class Program
{
static void Main()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Battery");

foreach (ManagementObject queryObj in searcher.Get())
{
PropertyDataCollection properties = queryObj.Properties;

foreach (PropertyData property in properties)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
}
}
}
However, this code does not provide all the desired information about the battery. I need to retrieve additional details like the current battery capacity and designed battery capacity. How can I modify the code to obtain these missing or additional battery information? Here's the sample output from the current code:
Availability: 2
BatteryRechargeTime:
BatteryStatus: 2
Caption: Internal Battery
Chemistry: 2
ConfigManagerErrorCode:
ConfigManagerUserConfig:
CreationClassName: Win32_Battery
Description: Internal Battery
DesignCapacity:
DesignVoltage: 12822
DeviceID: 5650CelxpertL19C3PF3
ErrorCleared:
ErrorDescription:
EstimatedChargeRemaining: 88
EstimatedRunTime: 71582788
ExpectedBatteryLife:
ExpectedLife:
FullChargeCapacity:
InstallDate:
LastErrorCode:
MaxRechargeTime:
Name: L19C3PF3
PNPDeviceID:
PowerManagementCapabilities: System.UInt16[]
PowerManagementSupported: False
SmartBatteryVersion:
Status: OK
StatusInfo:
SystemCreationClassName: Win32_ComputerSystem
SystemName: LAPTOP
TimeOnBattery:
TimeToFullCharge:
Availability: 2
BatteryRechargeTime:
BatteryStatus: 2
Caption: Internal Battery
Chemistry: 2
ConfigManagerErrorCode:
ConfigManagerUserConfig:
CreationClassName: Win32_Battery
Description: Internal Battery
DesignCapacity:
DesignVoltage: 12822
DeviceID: 5650CelxpertL19C3PF3
ErrorCleared:
ErrorDescription:
EstimatedChargeRemaining: 88
EstimatedRunTime: 71582788
ExpectedBatteryLife:
ExpectedLife:
FullChargeCapacity:
InstallDate:
LastErrorCode:
MaxRechargeTime:
Name: L19C3PF3
PNPDeviceID:
PowerManagementCapabilities: System.UInt16[]
PowerManagementSupported: False
SmartBatteryVersion:
Status: OK
StatusInfo:
SystemCreationClassName: Win32_ComputerSystem
SystemName: LAPTOP
TimeOnBattery:
TimeToFullCharge:
2 replies