I need To Build json of the following structure:

[{
  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..
 ] 

I'm Using Vb.Net , SQl Server To get the menus and Items (objects) from the database and convert the data to json string using this code:

    '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 &= "]"

A Sample of the Resulted Json string in vb.Net:

[{"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},]"}
 ,]"}

Why Backslashes are added only to the json on 'Children' Property? Do you think that the double Serialization for the children parameter is causing the problem? and if that's is the real reason how can I concatenate children jsons to the main json strings?

0
userx 19 November 2017, 14:49

1 menjawab

Jawaban Terbaik

I pushed Each Children into a List Of Object then I converted the list to Array after that I serialized the main json, The Code:

 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
0
userx 19 November 2017, 12:44