Foros del Web » Programando para Internet » ASPX (.net) »

problema al crear json con webservice

Estas en el tema de problema al crear json con webservice en el foro de ASPX (.net) en Foros del Web. hola a todos tengo un problema al crear un json desde un web service. He estado intentando durante todo el dia, y no he podido ...
  #1 (permalink)  
Antiguo 15/08/2012, 20:13
 
Fecha de Ingreso: enero-2009
Mensajes: 201
Antigüedad: 15 años, 3 meses
Puntos: 1
problema al crear json con webservice

hola a todos



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
  1. Imports System.Web.Services
  2. Imports System.Web.Services.Protocols
  3. Imports System.ComponentModel
  4. Imports System.Web.Script.Serialization
  5. Imports System.Web.Script.Services
  6. Imports System.Collections
  7. Imports System.Web
  8. Imports Newtonsoft.Json
  9.  
  10. <System.Web.Services.WebService(Namespace:="http://www.williamsportwebdeveloper.com/", Description:="Web services to query the book database.")> _
  11. <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
  12. <ToolboxItem(False)> _
  13. <ScriptService()> _
  14. Public Class actividades
  15.     Inherits System.Web.Services.WebService
  16.     Private cGantt As New ClassDatosGantt
  17.  
  18.     <WebMethod()> _
  19.     Public Function getActividades() As String
  20.         Dim tareas As New jsonTareas
  21.         Dim dv As New DataView(cGantt.getActividades(1968))
  22.         Dim cadena As String = ""
  23.         If dv.Count = 0 Then Return "[]"
  24.  
  25.         For i As Integer = 0 To dv.Count - 1
  26.             With dv.Item(i)
  27.                 tareas.id = .Item("id_planactividades")
  28.                 tareas.name = .Item("actividad")
  29.                 tareas.StartDate = .Item("fecInicio")
  30.                 tareas.EndDate = .Item("fectermino")
  31.                 tareas.BaselineStartDate = .Item("fecInicioReal")
  32.                 tareas.BaselineEndDate = .Item("fecTerminoReal")
  33.                 tareas.Priority = .Item("prioridad")
  34.                 tareas.PercentDone = .Item("avance")
  35.                 tareas.BaselinePercentDone = .Item("avanceReal")
  36.                 tareas.Responsible = ""
  37.                 tareas.expanded = False
  38.                 tareas.children = getHijos(.Item("id_planactividades"))
  39.             End With
  40.             cadena &= tareas.ToJSON.ToString & ","
  41.         Next
  42.  
  43.         cadena = cadena.Substring(0, cadena.Length - 1)
  44.         Return "[" & cadena & "]"
  45.     End Function
  46.  
  47.     Function getHijos(ByVal idPadre As Integer) As String
  48.         Dim tareas As New jsonTareas
  49.         Dim arrTares As New ArrayList
  50.         Dim dv As New DataView(cGantt.getActividadesHijos(idPadre))
  51.         Dim cadena As String = ""
  52.  
  53.         If dv.Count = 0 Then Return ""
  54.  
  55.         For i As Integer = 0 To dv.Count - 1
  56.             With dv.Item(i)
  57.                 tareas.id = .Item("id_planactividades")
  58.                 tareas.name = .Item("actividad")
  59.                 tareas.StartDate = .Item("fecInicio")
  60.                 tareas.EndDate = .Item("fectermino")
  61.                 tareas.BaselineStartDate = .Item("fecInicioReal")
  62.                 tareas.BaselineEndDate = .Item("fecTerminoReal")
  63.                 tareas.Priority = .Item("prioridad")
  64.                 tareas.PercentDone = .Item("avance")
  65.                 tareas.BaselinePercentDone = .Item("avanceReal")
  66.                 tareas.Responsible = ""
  67.                 tareas.expanded = False
  68.                 tareas.children = ""
  69.                 arrTares.Add(tareas.ToJSON)
  70.             End With
  71.             cadena &= tareas.ToJSON.ToString & ","
  72.         Next
  73.  
  74.         cadena = cadena.Substring(0, cadena.Length - 1)
  75.         Return cadena
  76.     End Function
  77. End Class
  78.  
  79. Public Class jsonTareas
  80.     Public id As Integer
  81.     Public name As String
  82.     Public PercentDone As Integer
  83.     Public Documento As String
  84.     Public BaselinePercentDone As Integer
  85.     Public Priority As Integer
  86.     Public Responsible As String
  87.     Public StartDate As String
  88.     Public EndDate As String
  89.     Public BaselineStartDate As String
  90.     Public BaselineEndDate As String
  91.     Public expanded As Boolean
  92.     Public children As String
  93.  
  94.     Public Function ToJSON() As String
  95.         Dim jsonSerializer As New System.Web.Script.Serialization.JavaScriptSerializer()
  96.         Return jsonSerializer.Serialize(Me)
  97.     End Function
  98.  
  99. End Class





Lo cual me trae los siguientes datos:

Código Javascript:
Ver original
  1. [{"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:
Ver original
  1. [
  2.     {    
  3.         "EndDate" : "2012-03-08",
  4.         "BaselineEndDate" : "2012-03-08",
  5.         "Id" : 5,
  6.         "leaf" : true,
  7.         "Name" : "Customer approval",
  8.         "PercentDone" : 0,
  9.         "BaselinePercentDone" : 0,
  10.         "Priority" : 2,
  11.         "Responsible" : "",
  12.         "BaselineStartDate" : "2012-03-08",
  13.         "StartDate" : "2012-03-08"
  14.     },
  15.     {
  16.         "EndDate" : "2012-03-18",
  17.         "BaselineEndDate" : "2012-03-18",
  18.         "Id" : 6,
  19.         "Name" : "Implementation Phase 2",
  20.         "PercentDone" : 50,
  21.         "BaselinePercentDone" : 30,
  22.         "Priority" : 1,
  23.         "Responsible" : "",
  24.         "BaselineStartDate" : "2012-03-08",
  25.         "StartDate" : "2012-03-08",
  26.         "expanded" : true,
  27.         "children" : [
  28.             {
  29.                 "EndDate" : "2012-03-18",
  30.                 "BaselineEndDate" : "2012-03-18",
  31.                 "Id" : 25,
  32.                 "leaf" : true,
  33.                 "Name" : "Task 3",
  34.                 "PercentDone" : 10,
  35.                 "BaselinePercentDone" : 10,
  36.                 "Priority" : 0,
  37.                 "Responsible" : "",
  38.                 "BaselineStartDate" : "2012-03-08",
  39.                 "StartDate" : "2012-03-08"
  40.               },
  41.               { "EndDate" : "2012-03-18",
  42.                 "BaselineEndDate" : "2012-03-18",
  43.                 "Id" : 26,
  44.                 "leaf" : true,
  45.                 "Name" : "Task 2",
  46.                 "PercentDone" : 20,
  47.                 "Priority" : 0,
  48.                 "Responsible" : "",
  49.                 "BaselineStartDate" : "2012-03-08",
  50.                 "StartDate" : "2012-03-08"
  51.               },
  52.               { "EndDate" : "2012-03-18",
  53.                 "BaselineEndDate" : "2012-03-18",
  54.                 "Id" : 27,
  55.                 "leaf" : true,
  56.                 "Name" : "Task 1",
  57.                 "PercentDone" : 20,
  58.                 "BaselinePercentDone" : 0,
  59.                 "Priority" : 0,
  60.                 "Responsible" : "",
  61.                 "StartDate" : "2012-03-08",
  62.                 "BaselineStartDate" : "2012-03-08"
  63.               }
  64.         ]
  65.     },
  66.     {
  67.         "EndDate" : "2012-03-17",
  68.         "BaselineEndDate" : "2012-03-17",
  69.         "Id" : 10,
  70.         "leaf" : true,
  71.         "Name" : "Customer approval 2",
  72.         "PercentDone" : 0,
  73.         "BaselinePercentDone" : 0,
  74.         "Priority" : 1,
  75.         "Responsible" : "",
  76.         "StartDate" : "2012-03-17",
  77.         "BaselineStartDate" : "2012-03-17"
  78.     },
  79.     {
  80.         "EndDate" : "2012-05-08",
  81.         "BaselineEndDate" : "2012-05-08",
  82.         "Id" : 8,
  83.         "Name" : "Production phase 1",
  84.         "PercentDone" : 40,
  85.         "BaselinePercentDone" : 0,
  86.         "Priority" : 2,
  87.         "Responsible" : "",
  88.         "StartDate" : "2012-03-22",
  89.         "BaselineStartDate" : "2012-03-22",
  90.         "expanded" : true,
  91.         "children" : [
  92.             {
  93.                 "EndDate" : "2012-04-07",
  94.                 "BaselineEndDate" : "2012-04-07",
  95.                 "Id" : 22,
  96.                 "leaf" : true,
  97.                 "Name" : "Assemble",
  98.                 "PercentDone" : 50,
  99.                 "BaselinePercentDone" : 30,
  100.                 "Priority" : 1,
  101.                 "Responsible" : "",
  102.                 "StartDate" : "2012-03-22",
  103.                 "BaselineStartDate" : "2012-03-22"
  104.             }
  105.         ]
  106.     },
  107.     {
  108.         "EndDate" : "2012-05-15",
  109.         "BaselineEndDate" : "2012-05-11",
  110.         "Id" : 9,
  111.         "leaf" : true,
  112.         "Name" : "Final testing",
  113.         "PercentDone" : 0,
  114.         "BaselinePercentDone" : 0,
  115.         "Priority" : 1,
  116.         "Responsible" : "",
  117.         "BaselineStartDate" : "2012-05-02",
  118.         "StartDate" : "2012-05-07"
  119.     },
  120.     {
  121.         "EndDate" : "2012-05-14",
  122.         "BaselineEndDate" : "2012-05-11",
  123.         "Id" : 7,
  124.         "leaf" : true,
  125.         "Name" : "Delivery",
  126.         "PercentDone" : 40,
  127.         "BaselinePercentDone" : 30,
  128.         "Priority" : 1,
  129.         "Responsible" : "",
  130.         "BaselineStartDate" : "2012-05-11",
  131.         "StartDate" : "2012-05-14"
  132.     }
  133. ]
¿algiien sabe como puedo hacer para que el json me salga de la forma que necesito?
__________________
Francisco Birrer
Arquitecto de Software e-process
http://www.universite.cl

Etiquetas: javascript, json, webservice
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:26.