我想把我的数据序列化为这种格式:
{
"6902083881405": {
"name": "娃哈哈饮用纯净水",
"spec": "596ML",
"price": 1.5,
"unit": "瓶"
},
"6902083893736": {
"name": "娃哈哈营养快线原味",
"spec": "350ML",
"price": 3.5,
"unit": "瓶"
}
}
--------------------------------------------------------------------
可是我怎么弄都只会弄成这样:
[
{
"code": "6902083881405",
"name": "娃哈哈饮用纯净水",
"spec": "596ML",
"price": 1.5,
"unit": "瓶"
},
{
"code": "6902083893736",
"name": "娃哈哈营养快线原味",
"spec": "350ML",
"price": 3.5,
"unit": "瓶"
}
]
请高手指点一二,我想生成上面那种数据格式,那种可读性更强,先谢谢了!
------解决思路----------------------
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dic As Dictionary(Of String, info) = New Dictionary(Of String, info)
dic.Add("1111", New info("1", "2", 1.1, "3"))
dic.Add("2222", New info("1", "2", 1.1, "3"))
dic.Add("3333", New info("1", "2", 1.1, "3"))
Dim jsonStr As String = Newtonsoft.Json.JsonConvert.SerializeObject(dic)
Console.WriteLine(jsonStr)
End Sub
End Class
Class info
Private m_name As String
Public Property name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property
Private m_spec As String
Public Property spec() As String
Get
Return m_spec
End Get
Set(ByVal value As String)
m_spec = value
End Set
End Property
Private m_price As Double
Public Property price() As Double
Get
Return m_price
End Get
Set(ByVal value As Double)
m_price = value
End Set
End Property
Private m_unit As String
Public Property unit() As String
Get
Return m_unit
End Get
Set(ByVal value As String)
m_unit = value
End Set
End Property
Sub New(ByVal name As String, ByVal spec As String, ByVal price As Double, ByVal unit As String)
Me.name = name
Me.spec = spec
Me.price = price
Me.unit = unit
End Sub
End Class