如何使用json.net 3.5版本8和.NET compact 3.5反序列化json [英] How do I deserialize json using json.net 3.5 release 8 and .NET compact 3.5
问题描述
这里有一个有趣的项目。我正在尝试编写一个应用程序来运行.Net Compact 3.5的摩托罗拉扫描仪。我必须使用REST API,并且遇到了将json反序列化为我使用Json.Net 3.5 R8编写的类的问题(这是支持.Net Compact的Json.Net的最后一个版本)。以下是我写的课程:
公共 类 ResponseProperties
公共 属性状态( )作为 字符串
获取
返回 m_Status
结束 获取
设置( ByVal 值正如 字符串)
m_Status = value
结束 设置
结束 属性
私有 m_Status 作为 字符串
公开 属性 Nsid()作为 字符串
获取
返回 m_Nsid
结束 获取
设置( ByVal 值作为 字符串)
m_Nsid = value
结束 设置
结束 属性
私有 m_Nsid As 字符串
公共 属性消息() As 字符串
获取
返回 m_Message
结束 获取
设置( ByVal 值作为 字符串)
m_Message = value
结束 设置
结束 属性
私有 m_Message 作为 字符串
公共 支柱erty RecordType()作为 字符串
获取
返回 m_RecordType
结束 获取
设置( ByVal 值作为 字符串)
m_RecordType = value
结束 设置
结束 属性
私有 m_RecordType 作为 String
公共 属性 BinNumber()正如 字符串
获取
返回 m_BinNumber
结束 获取
设置( ByVal 值作为 字符串)
m_BinNumber = value
结束 设置
结束 属性
私有 m_BinNumber 作为 字符串
公共 属性 ExternalID()作为 整数
< span class =code-keyword>获取
返回 m_ExternalID
结束 获取
设置( ByVal 值 As 整数)
m_ExternalID = value
结束 设置
结束 属性
私有 m_ExternalID 作为 整数
结束 类
公开 类响应
公共 属性 responce() As ResponseProperties()
获取
返回 m_responce
结束 获取
设置( ByVal 值 As ResponseProperties())
m_responce = value
结束 设置
结束 属性
私有 m_responce 正如 ResponseProperties()
结束 类
这里是我用来反序列化json的代码:
< pre lang =vb> Imports Newtonsoft.Json
Imports System.Net
Imports System.IO
Imports System.Text
Public 类 Form1
私有 Sub Button3_Click( ByVal sender As System。 Object , ByVal e As System.EventArgs) Handles Button3.Click
Dim Json As String = txtjson.Text
Dim ResponcesRes = JsonConvert.DeserializeObject( Of 回应)(Json)
lblOutput.Text = output& vbNewLine& ResponcesRes.responce( 1 )。BinNumber
结束 Sub
End Class
这是我收到的Json的一个例子。
{response:[{status:success,nsid:22222,message:message here,recordtype:bin,type:创建,binnumber:binname,externalid:1111},{status:success,nsid:22222,message:message here,recordtype:bin ,类型:创建,binnumber:binname,externalid:1111},{status:success,nsid:22222,message:message here 记录类型: 宾, 类型: 创造, binnumber: BINNAME, 外部ID:1111},{ 地位: 成功, NSID: 22222,消息:message here,recordtype:bin,type:create,binnumber:binname,externalid:1111},{status:success,nsid :22222,message:message here,recordtype:bin,type :create,binnumber:binname,externalid:1111},{status:success,nsid:22222,message:message here,recordtype: bin,type:create,binnumber:binname,externalid:1111}]}
代码没有错误我只是没有任何输出。如果我=我将鼠标悬停在ResponcesRes上它告诉我它包含Nothing。
如果需要更多信息请询问。
我尝试了什么:
尝试更改类结构,但这没有帮助解决问题。
Quote:
Public属性responce()As ResponseProperties()
{response:[
在您的示例JSON中,该属性称为回复 s e
,而非回复 c ë
。
尝试更改属性名称以匹配JSON。
Hi,
Have an interesting project here. I am trying to write an app to go onto a Motorola scanner that runs .Net Compact 3.5. I have to work with a REST API and am having issues with Deserializing the json to the classes that I have written using Json.Net 3.5 R8 (this is the last version of Json.Net that supported .Net Compact). Below is the classes that I have written:
Public Class ResponseProperties
Public Property Status() As String
Get
Return m_Status
End Get
Set(ByVal value As String)
m_Status = value
End Set
End Property
Private m_Status As String
Public Property Nsid() As String
Get
Return m_Nsid
End Get
Set(ByVal value As String)
m_Nsid = value
End Set
End Property
Private m_Nsid As String
Public Property Message() As String
Get
Return m_Message
End Get
Set(ByVal value As String)
m_Message = value
End Set
End Property
Private m_Message As String
Public Property RecordType() As String
Get
Return m_RecordType
End Get
Set(ByVal value As String)
m_RecordType = value
End Set
End Property
Private m_RecordType As String
Public Property BinNumber() As String
Get
Return m_BinNumber
End Get
Set(ByVal value As String)
m_BinNumber = value
End Set
End Property
Private m_BinNumber As String
Public Property ExternalID() As Integer
Get
Return m_ExternalID
End Get
Set(ByVal value As Integer)
m_ExternalID = value
End Set
End Property
Private m_ExternalID As Integer
End Class
Public Class Response
Public Property responce() As ResponseProperties()
Get
Return m_responce
End Get
Set(ByVal value As ResponseProperties())
m_responce = value
End Set
End Property
Private m_responce As ResponseProperties()
End Class
and here is the code that I am using to deserialize the json:
Imports Newtonsoft.Json
Imports System.Net
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim Json As String = txtjson.Text
Dim ResponcesRes = JsonConvert.DeserializeObject(Of Response)(Json)
lblOutput.Text = "output" & vbNewLine & ResponcesRes.responce(1).BinNumber
End Sub
End Class
Here is an example of the Json that I am receiving.
{"response":[{"status":"success","nsid":"22222","message":"message here","recordtype":"bin","type":"create","binnumber":"binname","externalid":1111},{"status":"success","nsid":"22222","message":"message here","recordtype":"bin","type":"create","binnumber":"binname","externalid":1111},{"status":"success","nsid":"22222","message":"message here","recordtype":"bin","type":"create","binnumber":"binname","externalid":1111},{"status":"success","nsid":"22222","message":"message here","recordtype":"bin","type":"create","binnumber":"binname","externalid":1111},{"status":"success","nsid":"22222","message":"message here","recordtype":"bin","type":"create","binnumber":"binname","externalid":1111},{"status":"success","nsid":"22222","message":"message here","recordtype":"bin","type":"create","binnumber":"binname","externalid":1111}]}
The code does not Error I Just don't get any output. if i=I hover the mouse over the ResponcesRes it tells me that it contains Nothing.
If there is more information needed please ask.
What I have tried:
Have tried to changed the Class Structure but this has not helped resolve the issue.
Quote:Public Property responce() As ResponseProperties(){"response":[
In your sample JSON, the property is calledresponse
, notresponce
.
Try changing the property name to match the JSON.
这篇关于如何使用json.net 3.5版本8和.NET compact 3.5反序列化json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!