1

我研究过其他类似的问题,但我做的不对。我的 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
4

1 回答 1

0

I dont know .net but i can tell you it related to JAVA. You can relate this with .Net

When we need to serialize or deserialize our class objects to json or from json we need Gson,

In Java we generally import tha package Gson package create its object for example:-

Suppose i have a class User, whose object you wanna serialize. Now to do this

take the whole object covert it to JSON with some key name with the help of Gson

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

Now you have serialized it to JSON, While deserializing it

just create ur User object. Get Json Object from Json file

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

so now userObject2 has all the values that you serialized earlier.

if you are not familiar with JAVA then Read about Gson for .net or you can read this link as well http://james.newtonking.com/projects/json-net.aspx

于 2012-11-14T18:12:40.297 回答