如何将 PHP Doctrine Querybuilder DateTime 从 HTTPResponseMessage 解析为 C# DateTime?
How to parse PHP Doctrine Querybuilder DateTime from a HTTPResponseMessage to C# DateTime?
我正在尝试将 SQL 结果从我的 PHP 制作的网站传递到我使用 Xamarin Forms 制作的 C# 应用程序。我通过在我的 PHP 应用程序中创建一个 REST API 端点并因此通过我的 C# 应用程序调用 URL 来完成此操作。这个 returns 一个包含我的结果的 Json 数组。但是,我找不到任何方法来将我收到的日期时间对象解析为 c# datetime,因为它在我无法摆脱的日期时间后面有“.00000”。
SQL
$query = $this->createQueryBuilder('c')
->select('partial c.{id, image, title,contentType, createdOn }', 'partial cd.{id, data}', 'partial u.{id, email} ', 'partial ud.{id, screenName, profileImage} ')
->join('c.data', 'cd')
->join('c.user', 'u')
->join('u.data', 'ud')
->andWhere('c.user !=:user')
->setParameters(
array(
'user' => $this->janVanAllemanId
)
)
->orderBy('c.modifiedOn', 'DESC')
->setFirstResult($offset)
->setMaxResults($limit)
->getQuery();
$results = $query->getArrayResult();
JSON数组
{
"posts": [
{
"id": 3373,
"contentType": 6,
"createdOn": {
"date": "2019-05-21 14:00:14.000000",
"timezone_type": 3,
"timezone": "Europe/Amsterdam"
},
...
},
{
"id": 3371,
"contentType": 6,
"createdOn": {
"date": "2019-05-18 14:30:09.000000",
"timezone_type": 3,
"timezone": "Europe/Amsterdam"
},
...
}
]
}
如你所见 returns 一个包含 "date"、"timezone" 和"timezone_type"。我一辈子都无法使用它来将它解析为 C# DateTime 对象。
这是我试过的:
PostRepository.cs
public class PostRepository : Repository
{
public async Task<List<Post>> GetNotifications(int userId, int offset = 0, int limit = 16)
{
string[] queryParams = { "userId=" + userId, "offset=" + offset, "limit=" + limit };
string uri =
RestService.GetApiUrl(UrlExtension.POST_NOTIFICATIONS, queryParams);
List<Post> posts = new List<Post>();
HttpResponseMessage response = null;
response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
PostList postList = await response.Content.ReadAsAsync<PostList>();
posts = postList.Posts;
}
return posts as List<Post>;
}
}
PostList.cs
public class PostList
{
[JsonProperty("posts")]
public List<Post> Posts { get; set; }
public PostList()
{
}
}
*方法一:
通过 DateTime 属性,我尝试反序列化结果。
Post.cs
...
public class Post
{
[JsonProperty("createdOn")]
public DateTime CreatedOnDateTime{get;set;}
}
这显然不起作用,因为它不是有效的 Datetime 对象。所以我尝试了一个 JObject 并获得了你可以在我的 Json 数组中看到的 "date" 键:
Post.cs
...
public class Post
{
public DateTime CreatedOnDateTime {get;set;}
[JsonProperty("createdOn")]
public JObject CreatedOn{
set{
CreatedOnDateTime= DateTime.ParseExact(value["date"].ToString(),
"M/d/yyyy hh:mm",CultureInfo.CreateSpecificCulture("nl-NL"));
}
}
它成功转换为 JObject,但它生成一个异常,指出它不是转换为日期时间对象的有效字符串!我主要认为这不起作用的原因是因为 .00000 神奇地出现在我的日期后面?!
有人知道将我的 Json 结果转换为 C# DateTime 的方法吗?非常感谢您的帮助!
你可以使用这个:
CreatedOnDateTime = DateTime.Parse(value["date"].ToString(), CultureInfo.CreateSpecificCulture("nl-NL"));
我正在尝试将 SQL 结果从我的 PHP 制作的网站传递到我使用 Xamarin Forms 制作的 C# 应用程序。我通过在我的 PHP 应用程序中创建一个 REST API 端点并因此通过我的 C# 应用程序调用 URL 来完成此操作。这个 returns 一个包含我的结果的 Json 数组。但是,我找不到任何方法来将我收到的日期时间对象解析为 c# datetime,因为它在我无法摆脱的日期时间后面有“.00000”。
SQL
$query = $this->createQueryBuilder('c')
->select('partial c.{id, image, title,contentType, createdOn }', 'partial cd.{id, data}', 'partial u.{id, email} ', 'partial ud.{id, screenName, profileImage} ')
->join('c.data', 'cd')
->join('c.user', 'u')
->join('u.data', 'ud')
->andWhere('c.user !=:user')
->setParameters(
array(
'user' => $this->janVanAllemanId
)
)
->orderBy('c.modifiedOn', 'DESC')
->setFirstResult($offset)
->setMaxResults($limit)
->getQuery();
$results = $query->getArrayResult();
JSON数组
{
"posts": [
{
"id": 3373,
"contentType": 6,
"createdOn": {
"date": "2019-05-21 14:00:14.000000",
"timezone_type": 3,
"timezone": "Europe/Amsterdam"
},
...
},
{
"id": 3371,
"contentType": 6,
"createdOn": {
"date": "2019-05-18 14:30:09.000000",
"timezone_type": 3,
"timezone": "Europe/Amsterdam"
},
...
}
]
}
如你所见 returns 一个包含 "date"、"timezone" 和"timezone_type"。我一辈子都无法使用它来将它解析为 C# DateTime 对象。
这是我试过的:
PostRepository.cs
public class PostRepository : Repository
{
public async Task<List<Post>> GetNotifications(int userId, int offset = 0, int limit = 16)
{
string[] queryParams = { "userId=" + userId, "offset=" + offset, "limit=" + limit };
string uri =
RestService.GetApiUrl(UrlExtension.POST_NOTIFICATIONS, queryParams);
List<Post> posts = new List<Post>();
HttpResponseMessage response = null;
response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
PostList postList = await response.Content.ReadAsAsync<PostList>();
posts = postList.Posts;
}
return posts as List<Post>;
}
}
PostList.cs
public class PostList
{
[JsonProperty("posts")]
public List<Post> Posts { get; set; }
public PostList()
{
}
}
*方法一: 通过 DateTime 属性,我尝试反序列化结果。
Post.cs
...
public class Post
{
[JsonProperty("createdOn")]
public DateTime CreatedOnDateTime{get;set;}
}
这显然不起作用,因为它不是有效的 Datetime 对象。所以我尝试了一个 JObject 并获得了你可以在我的 Json 数组中看到的 "date" 键:
Post.cs
...
public class Post
{
public DateTime CreatedOnDateTime {get;set;}
[JsonProperty("createdOn")]
public JObject CreatedOn{
set{
CreatedOnDateTime= DateTime.ParseExact(value["date"].ToString(),
"M/d/yyyy hh:mm",CultureInfo.CreateSpecificCulture("nl-NL"));
}
}
它成功转换为 JObject,但它生成一个异常,指出它不是转换为日期时间对象的有效字符串!我主要认为这不起作用的原因是因为 .00000 神奇地出现在我的日期后面?!
有人知道将我的 Json 结果转换为 C# DateTime 的方法吗?非常感谢您的帮助!
你可以使用这个:
CreatedOnDateTime = DateTime.Parse(value["date"].ToString(), CultureInfo.CreateSpecificCulture("nl-NL"));