Trouble parsing Json into .net Object

2019-07-31 09:44发布

I've studied other questions like this and I'm not doing something right. My vb.net class is wrong or something. Should my class only try to represent from candidates on or what? I need help with deserializing this 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" : {

      }
    }
  ]
}

I am using the following code to deserialize:

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

And my vb.net class looks like this:

<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

标签: json parsing
1条回答
一纸荒年 Trace。
2楼-- · 2019-07-31 10:12

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

查看更多
登录 后发表回答