代码之家  ›  专栏  ›  技术社区  ›  userx

为什么子json包含反斜杠

  •  0
  • userx  · 技术社区  · 8 年前

    我需要构建以下结构的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从数据库中获取菜单和项(对象),并使用以下代码将数据转换为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中生成的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参数的双重序列化是导致问题的原因吗?如果这是真正的原因,我如何将子json连接到主json字符串?

    1 回复  |  直到 8 年前
        1
  •  0
  •   userx    8 年前

    我将每个子对象推入一个对象列表,然后将列表转换为数组,然后序列化主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