2017-06-22 14 views
1

私はページの読み込み時にデータをポストするindexpage.aspxを持っています。このページでは、私は今、私はここにクラスがnewsoft jsonを使わずに文字列の辞書から各キー値を取得

 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」または 'への呼び出しが成功だ、postapi機能を持つ支払いクラスを持つ文字列

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 

のリストを作成しました

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

は、上記のキーと値を返しているのとおりです。get_deposit_address'コマンドを使用すると、この(JSON)と同様の結果が得られます。今私の質問は、私は結果の値を取得し、それが私に "1BitcoinAddress"、 "pubkey"と私のデータベースに保存する(私はそれを保存することができます結果のキーの3つの値を取得する私のデータベース」。

ありがとう。

+1

[VBを使用したJSON http投稿応答の読み方](https://stackoverflow.com/questions/15979742/how-to-read-json-http-post-response-using-vb) –

+0

なぜそれを分割する必要がありますか?これもまた辞書です。 'ret(" result ")'の値を 'Dictionary(String、String)'にキャストできませんか? – Icepickle

+0

ret.item( "result")の別の辞書値にキャストする必要がありますか?私がそれをしたいのであれば、どうすれば辞書から3つの値を取得してdbに保存するのですか –

答えて

0

Dictionary(Of String, Object)JavaScriptSerializerはすでにresultキーの値のための第二の辞書を作成しておく必要がありとして、あなたが入ってくるJSONをデシリアライズさので。あなたが今しなければならない唯一の事を、辞書に格納された値を使用して、この例のように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 

、プログラムは次のように、コンソールに出力を提供します:

アドレス:1BitcoinAddress、pubkeyで:、dest_tag:あなたの質問の100

大きな問題はあります実際にJson.netを使わずに逆シリアル化を解決したいのはなぜですか? JavaScriptSerializerのためのウェブサイト、 上では、一度に言う:

Json.NETは、シリアライズとデシリアライズを使用する必要があります。 AJAX対応アプリケーションのシリアル化およびデシリアライズ機能を提供します。

+0

あなたのコードをありがとう、それを試みましたが、アドレスだけを返します、pubkeyとdest_tagの値は現在アドレスです....私はそれにもかかわらず、とにかく私はそれを働くことができるかどうかを試してみてください。 –

+0

newtonsft.jsonを使用してもエラーが返されました。 'Newtonsoft.Json.Linq.JObject'タイプのオブジェクトをキャストできません –

+0

@Dannywilliamsこれはあなたが望むものではありませんでしたか?私はあなたが提供しているjsonを使用しました。サンプルで見ることができるように、私はあなたの最初のコメントで何を意味するのか不明です。 2番目のコメントのために、JObjectは辞書/動的のちょうど派手な形式ですが、キーを使って任意のオブジェクトにアクセスすることもできます – Icepickle

関連する問題