Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
700 views
in Technique[技术] by (71.8m points)

regex - JSON.NET: Get Specific JSON Date Value

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"": ""([dw]*)""")
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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...