我需要构建以下结构的json:
[{
Id: M1
Name: Menu1
Checked: True
Children:[
{Id: I1 , Name: Item1 , Checked: true, view:true , write:true},
{Id: I2 , Name: Item2 , Checked: true, view:true , write:true},
.. etc
]
},
{
Id: M2
Name: Menu2
Checked: True
Children:[
{Id: I1 , Name: Item1 , Checked: true, view:true , write:true},
{Id: I2 , Name: Item2 , Checked: true, view:true , write:true},
.. etc
]
} , etc..
]
我正在使用Vb.Net,SQl Server从数据库中获取菜单和Items(对象),并使用以下代码将数据转换为json字符串:
'Get Menus
Dim menus = DataFactory.Instance.GetMenusAndObjects(connectionString)
'Create JSON String
Dim jsonSerializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim json As String = "["
For Each m In menus
Dim children As String = "["
Dim menu_checked As Boolean = True
For Each obj In m.Objects
Dim checked As Boolean = True
Dim view As Boolean = True
Dim write As Boolean = True
Dim link = DataFactory.Instance.GetProfileObjectLink(connectionString, profile_code, obj.ObjectCode)
If IsNothing(link) Then
view = False
write = False
checked = False
menu_checked = False
Else
If link.AllowToView.Equals("N") Then
view = False
menu_checked = False
End If
If link.AllowToModify.Equals("N") Then
write = False
menu_checked = False
End If
End If
children &= jsonSerializer.Serialize(New With {Key .Id = obj.ObjectCode,
.Name = obj.ObjectDesc,
.Checked = checked,
.View = view,
.Write = write}) & ","
Next
children &= "]"
json &= jsonSerializer.Serialize(New With {Key .Id = m.MenuCode,
.Name = m.MenuName,
.Checked = menu_checked,
.Children = children}) & ","
Next
If json.Length > 1 Then
json = json.Substring(0, json.Length - 1)
End If
json &= "]"
vb.Net中的结果Json字符串示例:
[{"Id":"APPLN","Name":"Application","Checked":false,"Children":"
[{\"Id\":\"CUSTOMER_SCR\",\"Name\":\"62- Clients/
Customers\",\"Checked\":true,\"View\":false,\"Write\":false},
{\"Id\":\"EMP_SCR\",\"Name\":\"10- Employee
Manager\",\"Checked\":true,\"View\":true,\"Write\":true},]"}
,]"}
为什么反斜杠只添加到'Children'属性的json上?您是否认为children参数的双序列化导致问题?如果这是真正的原因我如何将子jsons连接到主json字符串?
答案 0 :(得分:0)
我将每个孩子推入一个对象列表然后我将列表转换为数组后我将序列化了主要的json,代码:
Imports System.Web
Imports System.Web.Services
Imports System.Web.Script.Serialization
Public Class GetMenusAndObjects
Implements System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/plain"
'Get parameters
Dim profile_code = context.Request("profile_code")
Dim connectionString As String = UserIdentity.ClientConfig.ConnectionString
'Get Profiles
Dim menus = DataFactory.Instance.GetMenusAndObjects(connectionString)
'Create JSON String
Dim jsonSerializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim json As String = "["
For Each m In menus
' Dim children As String = "["
Dim children = New List(Of Object)
Dim menu_checked As Boolean = True
For Each obj In m.Objects
Dim checked As Boolean = True
Dim view As Boolean = True
Dim write As Boolean = True
Dim link = DataFactory.Instance.GetProfileObjectLink(connectionString, profile_code, obj.ObjectCode)
If IsNothing(link) Then
view = False
write = False
checked = False
menu_checked = False
Else
If link.AllowToView.Equals("N") Then
view = False
menu_checked = False
End If
If link.AllowToModify.Equals("N") Then
write = False
menu_checked = False
End If
End If
children.Add(New With {.Id = obj.ObjectCode,
.Name = obj.ObjectDesc,
.Checked = checked,
.View = view,
.Write = write
})
'children &= jsonSerializer.Serialize(New With {Key .Id = obj.ObjectCode,
' .Name = obj.ObjectDesc,
' .Checked = checked,
' .View = view,
' .Write = write}) & ","
Next
'children &= "]"
json &= jsonSerializer.Serialize(New With {Key .Id = m.MenuCode,
.Name = m.MenuName,
.Checked = menu_checked,
.Children = children.ToArray()}) & ","
Next
If json.Length > 1 Then
json = json.Substring(0, json.Length - 1)
End If
json &= "]"
'Return JSON in response
context.Response.Write(json)
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class