visual basic .net反序列化

时间:2016-11-20 10:27:30

标签: .net json deserialization

如何反序列化json文件,如下所示:

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

}

来自whati的

这是json"字典"但我不知道我读它和普通的json文件一样

离。正常的json:

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

我不知道它们表现得多么不同,因为我看着它,每个人都做同样的事情,但对我来说它并没有真正起作用

我的班级:

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,其中字符串我得到" ",我真的不知道问题出在哪里

1 个答案:

答案 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 Object也有问题。 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) 

我希望我能帮助你。