I'm using Blockchain.info API. I want to deserialize the JSON response. An example response:
{"addresses":[{"balance":1400938800,"address":"1Q1AtvCyKhtveGm3187mgNRh5YcukUWjQC","label":"SMS Deposits","total_received":5954572400},{"balance":79434360,"address":"1A8JiWcwvpY7tAopUkSnGuEYHmzGYfZPiq","label":"My Wallet","total_received":453300048335},{"balance":0,"address":"17p49XUC2fw4Fn53WjZqYAm4APKqhNPEkY","total_received":0}]}
I want to know how to make a class that fits this. I tried this but it's not working:
PublicFunctionLoadJson(ByValjsonAsString)DimitemsAsList(Of addresses)=JsonConvert.DeserializeObject(Of List(Of addresses))(json)ReturnitemsEndFunctionPublicClassaddressesPublicaddressesAsaddress()EndClassPublicClassaddressPublicbalancePublicaddressPubliclabelPublictotal_receivedEndClass
I found a simple solution for this:
Go to this link:http://json2csharp.com/paste your json and it will make your class and its dependency.
Now, you may create your new one in your app and the use simply javascript.serialize,deserialize nothing else and it will work perfectly
Here is a console application example working perfectly:
ImportsSystem.NetImportsSystem.IOImportsSystem.ConfigurationImportsSystem.Web.Script.SerializationModuleModule1DimjsAsNewJavaScriptSerializerSubMain()DimjsonAsString=String.Empty Using srAsNewStreamReader("C:\Users\Giorgio\Desktop\GOOGLEDRIVE\hpc\PROGETTI VS PERSONALI\VISUALSTUDIOPROJECT\2014\Smples\Smples\json.txt")json=sr.ReadToEndEndUsingDimseparatorAsString="**********************************************************"DimrootAsRootObject=js.Deserialize(Of RootObject)(json)ForEachelInroot.addresses Console.WriteLine(String.Format("{0}"&vbCrLf&"{1}"&vbCrLf&"{2}"&vbCrLf&"{3}"&vbCrLf&separator,el.address,el.balance,el.label,el.total_received))NextConsole.ReadLine()EndSubPublicClassAddressPublicPropertybalance()AsIntegerGetReturnm_balanceEndGetSet(valueAsInteger)m_balance=ValueEndSetEndPropertyPrivatem_balanceAsIntegerPublicPropertyaddress()AsStringGetReturnm_addressEndGetSet(valueAsString)m_address=ValueEndSetEndPropertyPrivatem_addressAsStringPublicPropertylabel()AsStringGetReturnm_labelEndGetSet(valueAsString)m_label=ValueEndSetEndPropertyPrivatem_labelAsStringPublicPropertytotal_received()AsObjectGetReturnm_total_receivedEndGetSet(valueAsObject)m_total_received=ValueEndSetEndPropertyPrivatem_total_receivedAsObjectEndClassPublicClassRootObjectPublicPropertyaddresses()AsList(Of Address)GetReturnm_addressesEndGetSet(valueAsList(Of Address))m_addresses=ValueEndSetEndPropertyPrivatem_addressesAsList(Of Address)EndClassEnd Module