You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何原样提取JObject中字段的原始字符串内容?

如何获取JSON中原始格式的日期字符串?

请看以下VB.NET代码:

Dim jsonString As String = "{
    ""uuid"": ""4b84ca0c-40c8-4c2e-b1af-ddd727f3d0cf"",
    ""side"": ""bid"",
    ""ord_type"": ""limit"",
    ""price"": ""0.00000012"",
    ""state"": ""wait"",
    ""market"": ""BTC-RLY"",
    ""created_at"": ""2024-07-12T07:27:12Z"",
    ""volume"": ""121638.62897338"",
    ""remaining_volume"": ""121638.62897338"",
    ""reserved_fee"": ""0.00006714452319330576"",
    ""remaining_fee"": ""0.00006714452319330576"",
    ""paid_fee"": ""0"",
    ""locked"": ""0.01466377999999890576"",
    ""executed_volume"": ""0"",
    ""trades_count"": 0,
    ""base123"": ""BTC"",
    ""quote123"": ""RLY""
}"

Dim b1 As JObject = JObject.Parse(jsonString)
Dim createdAt As String = b1("created_at").Value(Of String) '07/12/2024 07:27:12
Dim createdAt1 As String = b1("created_at").ToString '7/12/2024 7:27:12 AM
Dim createdAt2 As String = "2024-07-12T07:27:12Z" '<- 如何获取该值?

执行后各变量结果:

  • createdAt 的结果:
    "07/12/2024 07:27:12"
    
  • createdAt1 的结果:
    "7/12/2024 7:27:12 AM"
    
  • 原始JSON中的目标值:
    "2024-07-12T07:27:12Z"
    

直接调用 b1.ToString() 时,输出的JSON里created_at仍保留原始格式,但单独提取字段时却无法得到原始字符串,该怎么解决?


解决方法

方法1:通过JValue获取原始字符串

JObject解析ISO8601格式的日期字符串时,会自动将其转换为DateTime类型的JValue。要获取原始字符串,只需将节点转为JValue并读取OriginalString属性:

Dim createdAt2 As String = DirectCast(b1("created_at"), JValue).OriginalString

方法2:解析时禁用日期自动识别

如果不需要将日期字段转为DateTime类型,可以在解析JSON时设置DateParseHandling.None,这样所有日期格式的字符串都会被保留为原始字符串类型:

Dim settings As New JsonSerializerSettings() With {
    .DateParseHandling = DateParseHandling.None
}
Dim b1 As JObject = JObject.Parse(jsonString, New JsonLoadSettings() With {
    .DateParseHandling = DateParseHandling.None
})
Dim createdAt2 As String = b1("created_at").Value(Of String) ' 直接得到原始字符串

内容的提问来源于stack exchange,提问作者user4951

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 07:44:54