【问题标题】:How to simplify this code using newtonsoft or other json parsers?如何使用 newtonsoft 或其他 json 解析器简化此代码?
【发布时间】:2018-01-14 16:07:02
【问题描述】:

我有一个简单的 json

{"200567175963759": {  "pair": "esp_btc",  "type": "sell",  "amount": 2000000,  "rate": 1E-08,  "timestamp_created": "1498114417",  "status": 0}}

我想在不创建任何新类的情况下解析它。我想让它变得简单。

我正在使用我自己创建的 jsonhelper 类来解析它。它基本上是尝试在两个双引号之间找到第一件事并得到 200567175963759 这是订单 ID。获取参数只是在 "pair":" 和 "

之间找到一些东西

对于简单的 json,它可以正常工作。如何使用更好的解析器(如 newtonsoft)获取订单 ID,即 200567175963759 或时间戳。

我想知道我是否可以使用 newtonsoft json 做到这一点?

                Dim jsonstring = jsonHelper.stripWhiteSpace(order3.ToString) '{"200567175963759": {  "pair": "esp_btc",  "type": "sell",  "amount": 2000000,  "rate": 1E-08,  "timestamp_created": "1498114417",  "status": 0}}
                Dim orderid = fGetToken(order3.ToString, 1, """", """")
                Dim base = b
                Dim quote = key
                Dim typeOfOrder = jsonHelper.getParameter(jsonstring, "type")
                Dim amount = jsonHelper.getParameter(jsonstring, "amount")
                Dim rate = jsonHelper.getParameter(jsonstring, "rate")
                Dim timestamp_created = jsonHelper.getParameter(jsonstring, "timestamp_created")
                Dim order4 = OrdersAtExchange.createOrders(amount, base, quote, _exchange, timestamp_created, rate, orderid)
                _orders.Add(order4)

如果我尝试使用 newtonsoft 解析它,我会得到这个类型为的对象

            Dim order = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonorders)
            Dim order1 = CType(order, Newtonsoft.Json.Linq.JObject)
            Dim order2 = order1.Item("return").ToList

我查看了 Newtonsoft.Json.Linq.JObject 中的所有方法,我找不到任何将 json 中的字典结构转换为 generic.dictionary 的内容

有类似的东西。我试过了,但根本没有用。

所以我想知道是否有使用 newtonsoft 解析简单 json 的一些代码的实际示例?

【问题讨论】:

    标签: json vb.net linq parsing


    【解决方案1】:

    对象是类型字典,如果属性看起来像索引或键它可能是字典

        Dim JsonString As String = "{""200567175963759"": {  ""pair"": ""esp_btc"",  ""type"": ""sell"",  ""amount"": 2000000,  ""rate"": 1E-08,  ""timestamp_created"": ""1498114417"",  ""status"": 0}}"
        Dim JsonSettings = New Newtonsoft.Json.JsonSerializerSettings
        JsonSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
        Dim OutObject =  Newtonsoft.Json.JsonConvert.DeserializeObject(Of Dictionary(Of String, SampleType))(JsonString)
    
    Class SampleType
      Property pair As String
      Property type As String
      Property amount As String
      Property rate As String
    Property timestamp_created As String
    Property status As String
    End Class
    

    【讨论】:

    • 我不想使用类。我想我可以用 Jtoken Jobject 和 Jarray 做到这一点
    【解决方案2】:

    这是一个示例,展示了如何使用 Json.Net 的 LINQ-to-JSON API(JTokens、JObjects 等)解析 JSON

    Dim json As String =
        "{" &
        "  ""200567175963759"": {" &
        "    ""pair"": ""esp_btc""," &
        "    ""type"": ""sell""," &
        "    ""amount"": 2000000," &
        "    ""rate"": 1E-08," &
        "    ""timestamp_created"": ""1498114417""," &
        "    ""status"": 0" &
        "  }" &
        "}"
    
    Dim rootObject As JObject = JObject.Parse(json)
    
    For Each prop As JProperty In rootObject.Properties()
    
        Dim orderid As String = prop.Name
        Dim orderInfo As JObject = prop.Value
        Dim pair As String = orderInfo("pair").ToString()
        Dim typeOfOrder As String = orderInfo("type").ToString()
        Dim amount As Decimal = orderInfo("amount").ToObject(Of Decimal)
        Dim rate As Decimal = orderInfo("rate").ToObject(Of Decimal)
        Dim timestamp_created As String = orderInfo("timestamp_created").ToString()
        Dim status As Integer = orderInfo("status").ToObject(Of Integer)
    
        'etc. ...
    
    Next
    

    演示:https://dotnetfiddle.net/X9SPIE

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2021-06-10
      • 1970-01-01
      • 2016-06-16
      相关资源
      最近更新 更多