另一个JSON查询..
我有一个表格中的数据,我需要序列化,但我不想显示键,只是值..我目前正在使用此代码,它给出了键和值..
Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim rows As New List(Of Dictionary(Of String, Object))()
Dim row As Dictionary(Of String, Object)
For Each dr As DataRow In dt.Rows
row = New Dictionary(Of String, Object)()
For Each col As DataColumn In dt.Columns
row.Add(col.ColumnName, dr(col))
Next
rows.Add(row)
Next
Dim X = serializer.Serialize(rows)
Return
这给了我{"field1" : "data1", "field2" : "data2"},
{"field1" : "data1", "field2" : "data2"},
{"field1" : "data1", "field2" : "data2"}
但我想{"data1","data2},{"data1","data2},{"data1","data2}
任何人都可以建议..
提前谢谢..
答案 0 :(得分:0)
用于序列化反序列化Json字符串的VB.NET实用程序类
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Runtime.Serialization.Json
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
''' <summary>
''' JSON Serialization and Deserialization Assistant ClassS
''' </summary>
Public Class JsonHelper
''' <summary>
''' JSON Serialization
''' </summary>
Public Shared Function JsonSerializer(Of T)(ByVal obj As T) As String
Dim ser As New DataContractJsonSerializer(GetType(T))
Dim ms As New MemoryStream()
ser.WriteObject(ms, obj)
Dim jsonString As String = Encoding.UTF8.GetString(ms.ToArray())
ms.Close()
'Replace Json Date String
Dim p As String = "\\/Date\((\d+)\+\d+\)\\/"
Dim matchEvaluator As New MatchEvaluator(AddressOf ConvertJsonDateToDateString)
Dim reg As New Regex(p)
jsonString = reg.Replace(jsonString, matchEvaluator)
Return jsonString
End Function
''' <summary>
''' JSON Deserialization
''' </summary>
Public Shared Function JsonDeserialize(Of T)(ByVal jsonString As String) As T
'Convert "yyyy-MM-dd HH:mm:ss" String as "\/Date(1319266795390+0800)\/"
Dim p As String = "\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}"
Dim matchEvaluator As New MatchEvaluator(AddressOf ConvertDateStringToJsonDate)
Dim reg As New Regex(p)
jsonString = reg.Replace(jsonString, matchEvaluator)
Dim ser As New DataContractJsonSerializer(GetType(T))
Dim ms As New MemoryStream(Encoding.UTF8.GetBytes(jsonString))
Dim obj As T = DirectCast(ser.ReadObject(ms), T)
Return obj
End Function
''' <summary>
''' Convert Serialization Time /Date(1319266795390+0800) as String
''' </summary>
Private Shared Function ConvertJsonDateToDateString(ByVal m As Match) As String
Dim result As String = String.Empty
Dim dt As New DateTime(1970, 1, 1)
dt = dt.AddMilliseconds(Long.Parse(m.Groups(1).Value))
dt = dt.ToLocalTime()
result = dt.ToString("yyyy-MM-dd HH:mm:ss")
Return result
End Function
''' <summary>
''' Convert Date String as Json Time
''' </summary>
Private Shared Function ConvertDateStringToJsonDate(ByVal m As Match) As String
Dim result As String = String.Empty
Dim dt As DateTime = DateTime.Parse(m.Groups(0).Value)
dt = dt.ToUniversalTime()
Dim ts As TimeSpan = dt - DateTime.Parse("1970-01-01")
result = String.Format("\/Date({0}+0800)\/", ts.TotalMilliseconds)
Return result
End Function
End Class
答案 1 :(得分:0)
还有
这是将JSON反序列化为对象的最简单方法(使用.NET 4):
Dim j As Object = New JavaScriptSerializer().Deserialize(Of Object)(JSONString)
然后,根据新对象的结构,您可以像这样检索数据:
r.status = j("status")
r.legs= j("routes")(0)("legs")
以及更详细的解决方案 :
我建议您在 DataContractJsonSerializer 上使用 JavaScriptSerializer 。原因是:
JavaScriptSerializer 比DataContractJsonSerializer更快
DataContractJsonSerializer 需要比JavaScriptSerializer更多的代码才能进行简单的序列化。
您不需要将DataContract和DataMember属性与JavaScriptSerializer一起使用
使用此数据类
<Serializable> _
Public Class ApplicationItem
Public Property application_id() As String
Get
Return m_application_id
End Get
Set
m_application_id = Value
End Set
End Property
Private m_application_id As String
Public Property application_package() As String
Get
Return m_application_package
End Get
Set
m_application_package = Value
End Set
End Property
Private m_application_package As String
End Class
并使用它来反序列化你的jsonText:
Dim jss As New JavaScriptSerializer()
Dim dict = jss.Deserialize(Of List(Of ApplicationItem))(jsonText)
如果您仍想使用DataContractJsonSerializer,可以使用以下代码反序列化:
Dim obj As New List(Of ApplicationItem)()
Dim ms As New MemoryStream(Encoding.Unicode.GetBytes(json))
Dim serializer As New System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.[GetType]())
obj = DirectCast(serializer.ReadObject(ms), List(Of ApplicationItem))
ms.Close()
ms.Dispose()