尝试反序列化JSON对象时收到此错误消息
错误消息
操作符未为字符串“JPY:”定义,并且键入
字典(Of String, Object)
。
下面是我试图反序列化的json对象
{"JPY":{"7d":"6510.92","30d":"4434.76","24h":"7027.46"},"USD":{"7d":"65.09","30d":"48.77","24h":"74.26"},"AUD":{"7d":"64.99","30d":"49.10","24h":"71.91"},"CHF":{"7d":"66.86","30d":"58.14","24h":"69.57"},"RUB":{"7d":"1896.79","30d":"1472.11","24h":"2206.51"},"时间戳": 1364257203,"THB":{"30d":"1100.31"},"CNY":{"7d":"417.58"":"16719.16","30d":"11865.62","24h":"19185.45"},"DKK":{"7d":"388.47","30d":"350.27","24h":"436.34"},"BRL":{"7d":"166.21","30d":"106.56","24h":"182.34"},"ILS":{"7d":"236.79","30d":"218.19","24h":"259.00"},"GBP":{"7d":"44.16","30d":"31.73","24h":"49.22"},"NZD":{"7d":"80.21","30d":"59.29","24h":"88.60"},"PLN":{"7d":"218{"7d":"68.76","30d":"50.16","24h":"76.43"},"SEK":{"7d":"445.63","30d":"315.11","24h":"510.58"},"SGD":{"7d":"87.88","30d":"61.97","24h":"93.35"},"HKD":{"7d":"550.10","30d":"396.68","24h":"571.82"},"EUR":{"7d":"51.85","30d":"38.65","24h":"58.69"}}
这是我的代码
Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Dim testObj = js.Deserialize(rt, New Object().GetType())
out = "JPY: " + testObj("JPY") + Environment.NewLine
out += "7days: " + testObj("7d") + Environment.NewLine
out += "30days: " + testObj("30d") + Environment.NewLine
out += "24hours: " + testObj("24h")
请使用调试器,它可以显示反序列化对象的外观。这是一本字典字典,相当于VB。NET声明将是Dictionary(字符串的),Dictionary(字符串的,字符串的)。这就是正确的代码:
Dim currency = testObj("JPY")
out = "JPY: " + Environment.NewLine
out += "7days: " + currency("7d") + Environment.NewLine
out += "30days: " + currency("30d") + Environment.NewLine
out += "24hours: " + currency("24h")