json反序列化,具有可更改的类名

时间:2015-08-24 09:43:26

标签: json vb.net

我知道如何使用JavaScriptSerializer反序列化java,但是由于这个json字符串,它不能正常工作

这是json字符串:

{"hell": {
   "id": 31930845,
   "name": "Hell",
   "profileIconId": 550,
   "revisionDate": 1440028482000,
   "summonerLevel": 30
}}

这是我的班级:

Public Class Summoner
    Public Property id As Integer
    Public Property name As String
    Public Property profileIconId As Integer
    Public Property revisionDate As Long
    Public Property summonerLevel As Integer
End Class

我的代码

Dim j As New JavaScriptSerializer
 Dim o As Summoner = j.Deserialize(Of Summoner)(json)

我继续从id获取空值,例如从名称

获取空字符串

1 个答案:

答案 0 :(得分:1)

你的班级结构有点不完整。而是尝试 -

public class Summoner
{
    public int id { get; set; }
    public string name { get; set; }
    public int profileIconId { get; set; }
    public long revisionDate { get; set; }
    public int summonerLevel { get; set; }
}

public class RootObject
{
    public Summoner hell { get; set; }
}

var t = new StreamReader("jsonpath");
var d = JsonConvert.DeserializeObject<RootObject>(t.ReadToEnd());

enter image description here