将 Json 解析为 .net 对象时遇到问题 [英] Trouble parsing Json into .net Object

查看:64
本文介绍了将 Json 解析为 .net 对象时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我研究了其他类似的问题,但我没有做对.我的 vb.net 类是错误的什么的.我的班级应该只尝试代表候选人还是什么?我需要反序列化这个 json 的帮助:

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

我的 vb.net 类看起来像这样:

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

推荐答案

我不知道 .net 但我可以告诉你它与 JAVA 相关.您可以将其与 .Net 联系起来

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

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

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

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

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

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

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

在 Gson 的帮助下将整个对象转换为 JSON 并带有一些键名

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

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

现在您已将其序列化为 JSON,反序列化时

Now you have serialized it to JSON, While deserializing it

只需创建您的用户对象.从 Json 文件中获取 Json 对象

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

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

所以现在 userObject2 拥有您之前序列化的所有值.

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

如果您不熟悉 JAVA,请阅读 Gson for .net或者您也可以阅读此链接 http://james.newtonking.com/projects/json-net.aspx

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

这篇关于将 Json 解析为 .net 对象时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆