故障解析JSON成.NET对象(Trouble parsing Json into .net Obj

2019-10-17 09:27发布

我研究过这样的其他问题,我没有做正确的事情。 我的vb.net类是错了或什么。 如果我的类只尝试从候选人或什么代表什么呢? 我需要反序列化JSON此帮助:

{
  "spatialReference" : {
    "wkid" : 2286
  },
  "candidates" : [
    {
      "address" : "100 MAIN ST",
      "location" : {
        "x" : 1144782.9490543604,
        "y" : 81361.525678694248
      },
      "score" : 100,
      "attributes" : {

      }
    },
    {
      "address" : "100 E MAIN ST",
      "location" : {
        "x" : 1120908.3257195801,
        "y" : 169917.71846333146
      },
      "score" : 77,
      "attributes" : {

      }
    }
  ]
}

我使用下面的代码进行反序列化:

Public Shared Function Deserialise(Of T)(ByVal json As String) As T
      Dim obj As T = Activator.CreateInstance(Of T)()

      Using ms As MemoryStream = New MemoryStream(Encoding.Unicode.GetBytes(json))
         Dim serializer As DataContractJsonSerializer = New       DataContractJsonSerializer(obj.GetType())
         obj = serializer.ReadObject(ms)
         Return obj
      End Using
   End Function

而我的vb.net类如下所示:

<DataContract()> _
Public Class SearchResults
   Private mCandidates() As candidate

   <DataContract()> _
   Public Class SpatialReference
      Private mwkId As String


      <DataMember()> _
      Public Property wkid() As String
         Get
            Return mwkId
         End Get
         Set(ByVal value As String)
            mwkId = value
         End Set
      End Property
   End Class

   <DataMember()> _
   Public Property Candidates() As candidate()
      Get
         Return mCandidates
      End Get
      Set(ByVal value As candidate())
         mCandidates = value
      End Set
   End Property
End Class

<DataContract()> _
   Public Class candidate
   Private mAddress As String
   Private mLocation As Location
   Private mScore As String
   Private mAttr As String

   <DataMember()> _
Public Property address() As String
      Get
         Return mAddress
      End Get
      Set(ByVal value As String)
         mAddress = value
      End Set
   End Property

   <DataMember()> _
   Public Property location() As Location
      Get
         Return mLocation
      End Get
      Set(ByVal value As Location)
         mLocation = value
      End Set
   End Property

   <DataMember()> _
   Public Property score() As String
      Get
         Return mScore
      End Get
      Set(ByVal value As String)
         mScore = value
      End Set
   End Property

   <DataMember()> _
   Public Property attributes() As String
      Get
         Return mAttr
      End Get
      Set(ByVal value As String)
         mAttr = value
      End Set
   End Property
End Class

Answer 1:

我不知道.NET,但我可以告诉你它与Java有关。 您可以与.net涉及本

当我们需要序列化或反序列化我们的类对象,JSON或JSON从我们所需要GSON,

在Java中,我们一般进口THA包GSON包创建它例如对象: -

假设我有一类用户,他们的对象,你想序列化。 现在要做到这一点

取整对象它GSON的帮助下隐蔽到JSON一些键名

jsonObject.put("KEYFORTHISjsonObject", gson.toJson(userClassObject));

现在你已经是序列化到JSON,虽然它反序列化

只是创建乌尔用户对象。 获得从JSON文件JSON对象

JSONObject jsonObject = new JSONObject(jsonFile.toString())
    userObject2 =  gson.fromJson(jsonObject.toString(), User.class);

所以现在userObject2有先前序列化的所有值。

如果你不熟悉JAVA然后阅读GSON为.NET或者您也可以阅读此链接,以及http://james.newtonking.com/projects/json-net.aspx



文章来源: Trouble parsing Json into .net Object
标签: json parsing