JSON.NET:获取特定的JSON日期值 [英] JSON.NET: Get Specific JSON Date Value

查看:175
本文介绍了JSON.NET:获取特定的JSON日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的VB.NET项目中,使用JSON.NET,我有一个来自Web API的JSON,其值代表 yyyy-MM-ddTHH中的日期:mm:ss 格式,我想简单地得到这个值。



这里或多或少是JSON的样子:

  
REQ_DATE:$ 1 $$$$$$$$$ 8:00 AM,
LEAVE_DATE:2016-01-23T00:00:00,
DUE_TIME:8:00 AM,
DUE_DATE $ 1 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ just just just just just just just just just just just just just just just just just just just just just just just just just序列化它,并用我的意志来做这件事,对吧?但是,当我将该键值放在一个变量中时,日期格式会自动更改!

  Dim temp As String = myJsonResult (REQ_DATE)
'temp =1/17/2016 12:27:57 PM

我需要将该Date作为检索的JSON。我看到两种方法来解决这个问题:手动将它转换为 yyyy-MM-ddTHH:mm:ss ,或使用正则表达式直接获取键值对其中我没有成功。



我尝试将其转换为DateTime:

  Dim tempDateTime As DateTime = DateTime.ParseExact(myJsonResult(REQ_DATE)。ToString,yyyy-MM-dd hh:mm:ss,CultureInfo.InvariantCulture)
'错误:String不被识别为有效DateTime
Dim myDesiredResult As String = tempDateTime.ToString(yyyy-MM-ddTHH:mm:ss)

我尝试使用正则表达式:

  Dim regex As Regex = New Regex( REQ_DATE:(\d\w*))
Dim match As Match = regex.Match(myJsonAsString)
如果match.Success然后
Dim myDesiredResult As String = match.Groups(1).Value
End If
'match is empty ...

任何和所有的帮助是非常感谢。

解决方案

我假设 myJsonResult 是一个您已经加载了JSON的JObject



您的困难是Json.NET自动将字符串识别为日期,并将其转换为 DateTime structs。然后,当您稍后在值令牌上执行 ToString()时,它以c#的不变文化格式返回,而不是原始格式,在这种情况下为 ISO 8601 。如果您不希望此行为,您可以使用 JsonSerializerSettings.DateParseHandling = DateParseHandling.None

  Dim jsonString ={' REQ_DATE':'2016-01-17T12:27:57','REQ_TYPE':'Vacation','HOURS':500.0,'LEAVE_TIME':'8:00 AM','LEAVE_DATE':' :00:00','DUE_TIME':'8:00 AM','DUE_DATE':'2016-01-24T00:00:00'}
Dim settings = New JsonSerializerSettings()With {_
.DateParseHandling = DateParseHandling.None _
}
Dim myJsonResult = JsonConvert.DeserializeObject(Of JObject)(jsonString,settings)
Dim dateValue = myJsonResult(REQ_DATE)
Dim dateString = CType(dateValue,String)'值为______ _ _ _ _ _ _ _ _ _ _ _ _ _没有超越广告到 JObject.Parse() ,它需要一个 JsonSerializerSettings ,所以你需要使用 DeserializeObject 。此设置最终会传播到 JsonReader.DateParseHandling



或者,如果您确定Json.NET可以识别日期,但始终希望以ISO 8601格式打印,您可以将令牌重新序列化为JSON,而不仅仅是获取字符串值:

  Dim dateValue = myJsonResult(REQ_DATE)
Dim dateString = JsonConvert.SerializeObject(dateValue).Trim(c)'值为... 1 1年1月17日27:57

原型小提琴。相关 c#问题


In my VB.NET project, using JSON.NET, I've got a JSON from a Web API that has a value representing a date in the yyyy-MM-ddTHH:mm:ss format, and I'd like to simply get that value.

Here's more or less what the JSON looks like:

{
    "REQ_DATE": "2016-01-17T12:27:57",
    "REQ_TYPE": "Vacation",
    "HOURS": 500.0,
    "LEAVE_TIME": "8:00 AM",
    "LEAVE_DATE": "2016-01-23T00:00:00",
    "DUE_TIME": "8:00 AM",
    "DUE_DATE": "2016-01-24T00:00:00",
}

So I should just serialize it and do what I will with the value, right? However, when I put that key-value in a variable, the date format is automatically changed!

Dim temp As String = myJsonResult("REQ_DATE")
' temp = "1/17/2016 12:27:57 PM"

I need to have that Date as it was from the retrieved JSON. I see two ways to go about this problem: converting it to yyyy-MM-ddTHH:mm:ss manually, or using regex to directly grab the key-value pair - both of which I have had no success with.

My attempt to convert it into DateTime:

Dim tempDateTime As DateTime = DateTime.ParseExact(myJsonResult("REQ_DATE").ToString,"yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture)
' Error: String is not Recognized as valid DateTime
Dim myDesiredResult As String = tempDateTime.ToString("yyyy-MM-ddTHH:mm:ss")

My attempt to use Regex:

Dim regex As Regex = New Regex("""REQ_DATE"": ""([\d\w]*)""")
Dim match As Match = regex.Match(myJsonAsString)
If match.Success Then
   Dim myDesiredResult As String = match.Groups(1).Value
End If
' match is empty... 

Any and all help is greatly appreciated.

解决方案

I assume that myJsonResult is a JObject into which you have loaded your JSON.

Your difficulty is that Json.NET automatically recognizes strings as dates when reading and converts them to DateTime structs. Then when you later do ToString() on the value token, it comes back in c#'s "invariant culture" format rather than the original format, which in this case was ISO 8601. If you do not want this behavior, you can parse your JSON using JsonSerializerSettings.DateParseHandling = DateParseHandling.None:

        Dim jsonString = "{'REQ_DATE':'2016-01-17T12:27:57','REQ_TYPE':'Vacation','HOURS':500.0,'LEAVE_TIME':'8:00 AM','LEAVE_DATE':'2016-01-23T00:00:00','DUE_TIME':'8:00 AM','DUE_DATE':'2016-01-24T00:00:00'}"
        Dim settings = New JsonSerializerSettings() With { _
            .DateParseHandling = DateParseHandling.None _
        }
        Dim myJsonResult = JsonConvert.DeserializeObject(Of JObject)(jsonString, settings)
        Dim dateValue = myJsonResult("REQ_DATE")
        Dim dateString = CType(dateValue, String) 'Value is 2016-01-17T12:27:57

There's no overload to JObject.Parse() that takes a JsonSerializerSettings, so you would need to use DeserializeObject. This setting eventually gets propagated to JsonReader.DateParseHandling.

Alternatively, if you are OK with Json.NET recognizing dates but would always like them to be printed in ISO 8601 format, you can re-serialize the token to JSON rather than just getting the string value:

        Dim dateValue = myJsonResult("REQ_DATE")
        Dim dateString = JsonConvert.SerializeObject(dateValue).Trim(""""c) 'Value is 2016-01-17T12:27:57

Prototype fiddle. Related c# question.

这篇关于JSON.NET:获取特定的JSON日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆