tengo un problema al crear un json desde un web service.
He estado intentando durante todo el dia, y no he podido dar con la solucion.
Les paso a comentar
El codigo que tengo es el siguiente:
Código vb:
Ver original
Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel Imports System.Web.Script.Serialization Imports System.Web.Script.Services Imports System.Collections Imports System.Web Imports Newtonsoft.Json <System.Web.Services.WebService(Namespace:="http://www.williamsportwebdeveloper.com/", Description:="Web services to query the book database.")> _ <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ <ToolboxItem(False)> _ <ScriptService()> _ Public Class actividades Inherits System.Web.Services.WebService Private cGantt As New ClassDatosGantt <WebMethod()> _ Public Function getActividades() As String Dim tareas As New jsonTareas Dim dv As New DataView(cGantt.getActividades(1968)) Dim cadena As String = "" If dv.Count = 0 Then Return "[]" For i As Integer = 0 To dv.Count - 1 With dv.Item(i) tareas.id = .Item("id_planactividades") tareas.name = .Item("actividad") tareas.StartDate = .Item("fecInicio") tareas.EndDate = .Item("fectermino") tareas.BaselineStartDate = .Item("fecInicioReal") tareas.BaselineEndDate = .Item("fecTerminoReal") tareas.Priority = .Item("prioridad") tareas.PercentDone = .Item("avance") tareas.BaselinePercentDone = .Item("avanceReal") tareas.Responsible = "" tareas.expanded = False tareas.children = getHijos(.Item("id_planactividades")) End With cadena &= tareas.ToJSON.ToString & "," Next cadena = cadena.Substring(0, cadena.Length - 1) Return "[" & cadena & "]" End Function Function getHijos(ByVal idPadre As Integer) As String Dim tareas As New jsonTareas Dim arrTares As New ArrayList Dim dv As New DataView(cGantt.getActividadesHijos(idPadre)) Dim cadena As String = "" If dv.Count = 0 Then Return "" For i As Integer = 0 To dv.Count - 1 With dv.Item(i) tareas.id = .Item("id_planactividades") tareas.name = .Item("actividad") tareas.StartDate = .Item("fecInicio") tareas.EndDate = .Item("fectermino") tareas.BaselineStartDate = .Item("fecInicioReal") tareas.BaselineEndDate = .Item("fecTerminoReal") tareas.Priority = .Item("prioridad") tareas.PercentDone = .Item("avance") tareas.BaselinePercentDone = .Item("avanceReal") tareas.Responsible = "" tareas.expanded = False tareas.children = "" arrTares.Add(tareas.ToJSON) End With cadena &= tareas.ToJSON.ToString & "," Next cadena = cadena.Substring(0, cadena.Length - 1) Return cadena End Function End Class Public Class jsonTareas Public id As Integer Public name As String Public PercentDone As Integer Public Documento As String Public BaselinePercentDone As Integer Public Priority As Integer Public Responsible As String Public StartDate As String Public EndDate As String Public BaselineStartDate As String Public BaselineEndDate As String Public expanded As Boolean Public children As String Public Function ToJSON() As String Dim jsonSerializer As New System.Web.Script.Serialization.JavaScriptSerializer() Return jsonSerializer.Serialize(Me) End Function End Class
Lo cual me trae los siguientes datos:
Código Javascript:
Ver original
[{"id":14,"name":"(Inicio) Diseño ","PercentDone":0,"Documento":null,"BaselinePercentDone":25,"Priority":1,"Responsible":"","StartDate":"01-08-2012","EndDate":"15-08-2012","BaselineStartDate":"01-08-2012","BaselineEndDate":"15-08-2012","expanded":false,"children":"{\"id\":15,\"name\":\"Ejecucion del proyecto\",\"PercentDone\":75,\"Documento\":null,\"BaselinePercentDone\":100,\"Priority\":1,\"Responsible\":\"\",\"StartDate\":\"16-08-2012\",\"EndDate\":\"31-08-2012\",\"BaselineStartDate\":\"27-08-2012\",\"BaselineEndDate\":\"31-08-2012\",\"expanded\":false,\"children\":\"\"}"},{"id":15,"name":"Ejecucion del proyecto","PercentDone":75,"Documento":null,"BaselinePercentDone":100,"Priority":1,"Responsible":"","StartDate":"16-08-2012","EndDate":"31-08-2012","BaselineStartDate":"27-08-2012","BaselineEndDate":"31-08-2012","expanded":false,"children":""},{"id":16,"name":"(Inicio) Cierre","PercentDone":100,"Documento":null,"BaselinePercentDone":0,"Priority":1,"Responsible":"","StartDate":"24-08-2012","EndDate":"31-08-2012","BaselineStartDate":"24-08-2012","BaselineEndDate":"31-08-2012","expanded":false,"children":""},{"id":17,"name":"(Inicio) Evaluacion post","PercentDone":0,"Documento":null,"BaselinePercentDone":25,"Priority":2,"Responsible":"","StartDate":"03-09-2012","EndDate":"14-09-2012","BaselineStartDate":"01-10-2012","BaselineEndDate":"19-10-2012","expanded":false,"children":""}]
Y deberia poder traer algo así:
Código Javascript:
¿algiien sabe como puedo hacer para que el json me salga de la forma que necesito? Ver original
[ { "EndDate" : "2012-03-08", "BaselineEndDate" : "2012-03-08", "Id" : 5, "leaf" : true, "Name" : "Customer approval", "PercentDone" : 0, "BaselinePercentDone" : 0, "Priority" : 2, "Responsible" : "", "BaselineStartDate" : "2012-03-08", "StartDate" : "2012-03-08" }, { "EndDate" : "2012-03-18", "BaselineEndDate" : "2012-03-18", "Id" : 6, "Name" : "Implementation Phase 2", "PercentDone" : 50, "BaselinePercentDone" : 30, "Priority" : 1, "Responsible" : "", "BaselineStartDate" : "2012-03-08", "StartDate" : "2012-03-08", "expanded" : true, "children" : [ { "EndDate" : "2012-03-18", "BaselineEndDate" : "2012-03-18", "Id" : 25, "leaf" : true, "Name" : "Task 3", "PercentDone" : 10, "BaselinePercentDone" : 10, "Priority" : 0, "Responsible" : "", "BaselineStartDate" : "2012-03-08", "StartDate" : "2012-03-08" }, { "EndDate" : "2012-03-18", "BaselineEndDate" : "2012-03-18", "Id" : 26, "leaf" : true, "Name" : "Task 2", "PercentDone" : 20, "Priority" : 0, "Responsible" : "", "BaselineStartDate" : "2012-03-08", "StartDate" : "2012-03-08" }, { "EndDate" : "2012-03-18", "BaselineEndDate" : "2012-03-18", "Id" : 27, "leaf" : true, "Name" : "Task 1", "PercentDone" : 20, "BaselinePercentDone" : 0, "Priority" : 0, "Responsible" : "", "StartDate" : "2012-03-08", "BaselineStartDate" : "2012-03-08" } ] }, { "EndDate" : "2012-03-17", "BaselineEndDate" : "2012-03-17", "Id" : 10, "leaf" : true, "Name" : "Customer approval 2", "PercentDone" : 0, "BaselinePercentDone" : 0, "Priority" : 1, "Responsible" : "", "StartDate" : "2012-03-17", "BaselineStartDate" : "2012-03-17" }, { "EndDate" : "2012-05-08", "BaselineEndDate" : "2012-05-08", "Id" : 8, "Name" : "Production phase 1", "PercentDone" : 40, "BaselinePercentDone" : 0, "Priority" : 2, "Responsible" : "", "StartDate" : "2012-03-22", "BaselineStartDate" : "2012-03-22", "expanded" : true, "children" : [ { "EndDate" : "2012-04-07", "BaselineEndDate" : "2012-04-07", "Id" : 22, "leaf" : true, "Name" : "Assemble", "PercentDone" : 50, "BaselinePercentDone" : 30, "Priority" : 1, "Responsible" : "", "StartDate" : "2012-03-22", "BaselineStartDate" : "2012-03-22" } ] }, { "EndDate" : "2012-05-15", "BaselineEndDate" : "2012-05-11", "Id" : 9, "leaf" : true, "Name" : "Final testing", "PercentDone" : 0, "BaselinePercentDone" : 0, "Priority" : 1, "Responsible" : "", "BaselineStartDate" : "2012-05-02", "StartDate" : "2012-05-07" }, { "EndDate" : "2012-05-14", "BaselineEndDate" : "2012-05-11", "Id" : 7, "leaf" : true, "Name" : "Delivery", "PercentDone" : 40, "BaselinePercentDone" : 30, "Priority" : 1, "Responsible" : "", "BaselineStartDate" : "2012-05-11", "StartDate" : "2012-05-14" } ]