错误消息:未为字符串“和类型”字典(字符串,对象)定义运算符“+”

时间:2013-03-26 00:32:48

标签: vb.net json serialization

尝试反序列化JSON对象时出现此错误消息

错误消息

未为字符串“JPY:”定义运算符+,并键入Dictionary(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“},”timestamp“:1364257203,”THB“:{”30d“:”1100.31“},”CNY“:{”7d“:”417.58“,”30d“:”324.35“,”24h“:” 466.92“},”SLL“:{”7d“:”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.66“,”30d“ :“163.68”,“24h”:“249.94”},“CAD”:{“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")

1 个答案:

答案 0 :(得分:0)

支持使用调试器,它可以显示反序列化对象的外观。它是字典的字典,等效的VB.NET声明是Dictionary(Of String,Of Dictionary(Of String,String))。这使得这个代码正确:

    Dim currency = testObj("JPY")
    out = "JPY: " + Environment.NewLine
    out += "7days: " + currency("7d") + Environment.NewLine
    out += "30days: " + currency("30d") + Environment.NewLine
    out += "24hours: " + currency("24h")