反序列化JSON文本 [英] Deserialise JSON text

查看:144
本文介绍了反序列化JSON文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图反序列化以下JSON文本,但无处可去,我无法弄清楚我哪里出错 - 有人能指出我正确的方向吗?



{

数据:

{

id:125,

domain:体育,

patient_name:Fred Smith,

patient_gender:男性,

patient_image:https :\ / \ / mysite.png,

温度: - 105,

持续时间:130,

weir_height:160,

session_rank:1,

next_session_rank:2,

next_session_date:

{

date:2017-10-15 10:00:00.000000,

timezone_type:3,

时区:UTC

}

}

}



我认为它与JSON字段是'数据'键的子级别有关,这就是我很难找到如何处理,因为代码使用的是没有的JSON数据这个容器r。



我尝试过:



使用响应As Net.HttpWebResponse = request.GetResponse 
使用streamReader作为新IO.StreamReader(response.GetResponseStream)

Dim jsonResponseText = streamReader.ReadToEnd
Dim jsonResult作为PFCNext =新的Web.Script.Serialization.JavaScriptSerializer()。反序列化(jsonResponseText,GetType(PFCNext))

结束使用'streamreader
结束使用'响应

类PFCNext
Property Id As String
Property Domain As String
Property Patient_name As String
Property Patient_gender As String
Property Patient_image As String
Property Temperature As String
Property Duration As String
Property Weir_height As String
Property Session_rank As String
Property Next_session_rank As String
Property Next_session_d ate As String
Property Date_ As String
Property Timezone_type As String
Property Timezone As String
End Class

解决方案

除非你编写自定义转换器,否则你的类的结构需要与你的JSON结构相匹配:

  PFCNextWrapper 
公共 属性数据作为 PFCNext
结束

PFCNext
公开 属性标识作为 整数
公开 属性 As 字符串
公共 属性 Patient_name 作为 字符串
公共 属性 Patient_gender 作为 字符串
公共 属性 Patient_image 作为 字符串
公共 属性温度 As 整数
公开 属性持续时间作为 整数
公共 属性 Weir_height 作为 整数
公共 属性 Session_rank As 整数
公开 属性 Next_session_rank 作为 整数
公共 属性 Next_session_date 作为 NextSessionDate
结束

NextSessionDate
公开 属性 [日期] 正如 日期
公开 属性 Timezone_type As 整数
公开 属性时区作为 字符串
结束



然后,您可以将JSON反序列化为 PFCNextWrapper 类型:

  Dim 序列化程序作为  Web.Script.Serialization.JavaScriptSerializer()
Dim jsonResult As PFCNextWrapper = CType ( serializer.Deserialize(jsonResponseText, GetType (PFCNextWrapper)),PFCNextWrapper)


此CodeProject文章将向您展示如何使用源代码获取原始内容JSON数据,快速生成类并反序列化数据:在C#和amp中使用JSON ; VB [ ^ ]

Im trying to deserialise the following JSON text, but getting nowhere and I cant figure out where Im going wrong - can anyone point me in the right direction please?

{
"data":
{
"id":125,
"domain":"Sports",
"patient_name":"Fred Smith",
"patient_gender":"male",
"patient_image":"https:\/\/mysite.png",
"temperature":-105,
"duration":130,
"weir_height":160,
"session_rank":1,
"next_session_rank":2,
"next_session_date":
{
"date":"2017-10-15 10:00:00.000000",
"timezone_type":3,
"timezone":"UTC"
}
}
}

I think its something to do with the JSON fields being a sub-level of the 'data' key and that is what I'm finding hard to figure out how to deal with as the code works with JSON data that doesn't have this container.

What I have tried:

Using response As Net.HttpWebResponse = request.GetResponse
     Using streamReader As New IO.StreamReader(response.GetResponseStream)
     
     Dim jsonResponseText = streamReader.ReadToEnd
     Dim jsonResult As PFCNext = New Web.Script.Serialization.JavaScriptSerializer().Deserialize(jsonResponseText, GetType(PFCNext))
     
    End Using 'streamreader
End Using 'response

    Class PFCNext
        Property Id As String
        Property Domain As String
        Property Patient_name As String
        Property Patient_gender As String
        Property Patient_image As String
        Property Temperature As String
        Property Duration As String
        Property Weir_height As String
        Property Session_rank As String
        Property Next_session_rank As String
        Property Next_session_date As String
        Property Date_ As String
        Property Timezone_type As String
        Property Timezone As String
    End Class

解决方案

Unless you write a custom converter, the structure of your class needs to match the structure of your JSON:

Class PFCNextWrapper
    Public Property Data As PFCNext
End Class

Class PFCNext
    Public Property Id As Integer
    Public Property Domain As String
    Public Property Patient_name As String
    Public Property Patient_gender As String
    Public Property Patient_image As String
    Public Property Temperature As Integer
    Public Property Duration As Integer
    Public Property Weir_height As Integer
    Public Property Session_rank As Integer
    Public Property Next_session_rank As Integer
    Public Property Next_session_date As NextSessionDate
End Class

Class NextSessionDate
    Public Property [Date] As Date
    Public Property Timezone_type As Integer
    Public Property Timezone As String
End Class


You can then deserialize your JSON to the PFCNextWrapper type:

Dim serializer As New Web.Script.Serialization.JavaScriptSerializer()
Dim jsonResult As PFCNextWrapper = CType(serializer.Deserialize(jsonResponseText, GetType(PFCNextWrapper)), PFCNextWrapper)


This CodeProject article will show you how, with source code, to take raw JSON data, generate the classes quickly and deserialize the data: Working with JSON in C# & VB[^]


这篇关于反序列化JSON文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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