从字符串对象字典中获取每个键值,而无需使用 newsoft json



我有一个索引页.aspx我在页面加载时将数据发布到索引页中。在此页面中,我创建了字符串列表

  Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim openWith As New SortedList(Of String, String)
    ' Add some elements to the list. There are no 
    ' duplicate keys, but some of the values are duplicates.
    openWith.Add("version", "1")
    openWith.Add("key", ConfigurationManager.AppSettings("publickey"))
    openWith.Add("cmd", "get_callback_address")
    openWith.Add("currency", "coin")
    Call POSTAPI("get_callback_address", openWith)
End Sub

现在我有一个具有postapi函数的支付类,这是类

     Public Shared Function POSTAPI(cmd As String, Optional parms As SortedList(Of String, String) = Nothing) As Dictionary(Of String, Object)
        Dim post_data As String = ""
        For Each parm As KeyValuePair(Of String, String) In parms
            If post_data.Length > 0 Then
                post_data += "&"
            End If
            post_data += parm.Key + "=" + Uri.EscapeDataString(parm.Value)
        Next
        Dim keyBytes As Byte() = encoding.GetBytes(s_privkey)
        Dim postBytes As Byte() = encoding.GetBytes(post_data)
        Dim hmacsha512 = New System.Security.Cryptography.HMACSHA512(keyBytes)
        Dim hmac As String = BitConverter.ToString(hmacsha512.ComputeHash(postBytes)).Replace("-", String.Empty)
        ' do the post:
        Dim cl As New System.Net.WebClient()
        cl.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
        cl.Headers.Add("HMAC", hmac)
        cl.Encoding = encoding
        Dim ret = New Dictionary(Of String, Object)()
        Try
            Dim resp As String = cl.UploadString("https://www.coinpayments.net/api.php", post_data)
            Dim decoder = New System.Web.Script.Serialization.JavaScriptSerializer()
            ret = decoder.Deserialize(Of Dictionary(Of String, Object))(resp)
        Catch e As System.Net.WebException
            ret("error") = "Exception while contacting CoinPayments.net: " + e.Message
        Catch e As Exception
            ret("error") = "Unknown exception: " + e.Message
        End Try
        Return ret

    End Function

它已成功发布,但成功调用"get_callback_address"或"get_deposit_address"命令将给出类似于以下内容的结果 (JSON(:

   {  
         "error":"ok",
         "result":{  
         "address":"1BitcoinAddress",
        "pubkey":"",
        "dest_tag":100,
         }
       }

以上是返回的键和值。现在我的问题是我只想获取结果的值并将其拆分,以便它给我"1BitcoinAddress"、"pubkey"并将其保存到我的数据库中(我想获取该结果键的 3 个值,以便我可以将其保存在我的数据库中"。

谢谢。

由于您将传入的 json 反序列化为Dictionary(Of String, Object)因此JavaScriptSerializer应该已经为 result 键的值创建了第二个字典。您现在唯一要做的就是创建一个变量,该变量获取存储在字典中的值并将其转换为Dictionary(Of String, Object)以便使用它,就像这个例子一样

Dim json As String = "{" +
"""error"":""ok""," +
  """result"":{" +
  """address"":""1BitcoinAddress""," +
  """pubkey"":""""," +
  """dest_tag"":100" +
  "}" +
     "}"
Dim deserializer = New System.Web.Script.Serialization.JavaScriptSerializer()
'get the full dictionary
Dim dictionary = deserializer.Deserialize(Of Dictionary(Of String, Object))(json)
' make sure there is a key in your dictionary
If dictionary.ContainsKey("result") Then
    'cast the value for "result" as a dictionary
    Dim resultDictionary As Dictionary(Of String, Object) = _
        DirectCast(dictionary("result"), Dictionary(Of String, Object))
    'you can then access the keys by their key
    Console.WriteLine("address: {0}, pubkey: {1}, dest_tag: {2}", _
        resultDictionary("address"), _
        resultDictionary("pubkey"), _
        resultDictionary("dest_tag"))
End If

通过您的输入,程序会在控制台上给出输出,如下所示:

地址: 1比特币地址, 公钥: , dest_tag: 100

你的问题最大的问题是为什么要在不使用 Json.net 的情况下解决反序列化问题。在JavaScriptSerializer的网站上, 它立即说:

Json.NET 应使用序列化和反序列化。为启用 AJAX 的应用程序提供序列化和反序列化功能。

最新更新