无法将Json解析为.net对象

时间:2012-11-14 17:53:43

标签: json parsing

我已经研究过这样的其他问题,而且我没有做对。我的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

1 个答案:

答案 0 :(得分:0)

我不知道.net但我可以告诉你它与JAVA有关。您可以将此与.Net

联系起来

当我们需要将类对象序列化或反序列化为json或json时,我们需要Gson,

在Java中我们通常导入包Gson包创建其对象,例如: -

假设我有一个类User,你想要序列化它的对象。现在要做到这一点

在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 for .net 或者您也可以阅读此链接http://james.newtonking.com/projects/json-net.aspx