2016-11-20 55 views
1

我如何反序列化JSON文件是这样的:的Visual Basic .NET反序列化

{ 
    "misterislukelis": { 
     "id": 44816005, 
     "name": "MisterisLukelis", 
     "profileIconId": 1391, 
     "summonerLevel": 30, 
     "revisionDate": 1479601967000 
} 

}

从whati聚集,这是JSON“字典”但我不知道我是否同它读成正常JSON文件

ex。正常的JSON:

[ { 
    "application_id":"1", 
    "application_package":"abc" 
    }, 
    { 
    "application_id":"2", 
    "application_package":"xyz" 
    } ] 

我不知道他们的行为diferently,becouse就像我在它看起来,每个人都只是做同样的事情,但对我来说并不真正工作

我的课:

Class summoner        'class opening 
    Public Property id As Integer   'summoner's id 
    Public Property name As String   'summoner's name 
    Public Property profileIconId As Integer 'profile icon id 
    Public Property summonerLevel As Integer 'summoner's level 
End Class         'end of a class 

,然后这应该反序列化JSON的:

Dim ser As JavaScriptSerializer = New JavaScriptSerializer()   
Dim jsonData As String = readData(URLadress)       'get  json file in to txt(i have function fo it, it works i triple checked) 
Dim summonerInfo As summoner = ser.Deserialize(Of summoner)(jsonData) 'deserialize json 
TextBox1.Text = summonerInfo.summonerLevel 'print out summoner level 

我看这应该工作正常,但所有我得到的整数应该是我得到0,其中字符串我得到“”,我真的不知道问题出在哪里

回答

0

第二个例子是一个JSON数组,并且可以正常使用反序列化JSON。

Dim webtest As String = "[ { 
    ""application_id"":""1"", 
    ""application_package"":""abc"" 
    }, 
    { 
    ""application_id"":""2"", 
    ""application_package"":""xyz"" 
    } ]" 
     Dim serializer As New JavaScriptSerializer() 
     Dim webtestdeserialized As New List(Of cSummoner) 
     webtestdeserialized = serializer.Deserialize(Of List(Of cSummoner))(webtest) '(serializedResult) 

我与JSON对象也有问题。 JSON对象我在一个对象中获取它,但不是在cSummoner列表中。

Dim webtest As String = "{ 
     ""id"": 44816005, 
     ""name"": ""MisterisLukelis"", 
     ""profileIconId"": 1391, 
     ""summonerLevel"": 30, 
     ""revisionDate"": 1479601967000 
}" 
     Dim serializer As New JavaScriptSerializer() 
     Dim webtestdeserialized As Object 
     webtestdeserialized = serializer.DeserializeObject(webtest) 

我希望我能帮助你。