none
Ayuda con Web Api Json no genera correcto el arbol RRS feed

  • Pregunta

  • Hola amigos, tengo una consulta que me esta rompiendo la cabeza desde hace un rato.

    Me pidieron que creara una Web API para enviar informacion por Json, me mandaron un modelo de como debe ir la informacion

    {"categories":[{"id":"12","title":"Grand Vitara SZ","slug":"grand-vitara-sz","intro_text":null,"intro_image":null,"full_text":"","full_image":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"3","created_at":"2017-07-18 17:53:29","modified_at":"2017-07-25 12:22:01","meta_title":"","meta_keywords":"","meta_description":"","vehicles_id":"0"}],"accessories":{"6":{"id":"6","title":"Molduras Laterales","slug":"molduras-laterales","code":"990E0-65J07","price":"288.00","image":"images\/accessories\/accessories\/molduras-laterales_grand-vitara-sz.jpg","description":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"6","created_at":"2017-07-25 12:24:13","modified_at":"2017-07-25 12:24:13","accessories_categories_id":"12"},"7":{"id":"7","title":"Salpicadero para llamanta","slug":"salpicadero-para-llamanta","code":"990E0-65J64","price":"72.08","image":"images\/accessories\/accessories\/salpicadero-para-llamanta_grand-vitara-sz.jpg","description":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"7","created_at":"2017-07-25 12:29:04","modified_at":"2017-07-25 13:19:57","accessories_categories_id":"12"},"8":{"id":"8","title":"Barra útilitaria de techo","slug":"barra-utilitaria-de-techo","code":"990E0-65J18","price":"346.81","image":"images\/accessories\/accessories\/barra-utilitaria-de-techo_grand-vitara-sz.jpg","description":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"8","created_at":"2017-07-25 13:20:43","modified_at":"2017-07-25 13:20:43","accessories_categories_id":"12"},"9":{"id":"9","title":"Bandera de Asiento","slug":"bandera-de-asiento","code":"99000-990N8-1","price":"53.33","image":"images\/accessories\/accessories\/bandera-de-asiento_grand-vitara-sz.jpg","description":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"9","created_at":"2017-07-25 13:22:14","modified_at":"2017-07-25 13:22:36","accessories_categories_id":"12"},"10":{"id":"10","title":"Candado Plástico","slug":"candado-plastico","code":"99000-99037-2","price":"8.47","image":"images\/accessories\/accessories\/candado-plastico_grand-vitara-sz.jpg","description":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"10","created_at":"2017-07-25 13:23:15","modified_at":"2017-07-25 13:23:15","accessories_categories_id":"12"},"11":{"id":"11","title":"Tuercas de Seguridad","slug":"tuercas-de-seguridad","code":"990E0-58-J49","price":"58.97","image":"images\/accessories\/accessories\/tuercas-de-seguridad_grand-vitara-sz.jpg","description":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"11","created_at":"2017-07-25 13:23:38","modified_at":"2017-07-25 13:23:47","accessories_categories_id":"12"},"12":{"id":"12","title":"Tubo Cromado","slug":"tubo-cromado","code":"99000-99069-3","price":"129.72","image":"images\/accessories\/accessories\/tubo-cromado_grand-vitara-sz.jpg","description":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"12","created_at":"2017-07-25 13:24:28","modified_at":"2017-07-25 13:24:28","accessories_categories_id":"12"},"13":{"id":"13","title":"Moqueta","slug":"moqueta","code":"99000-99037-6","price":"43.50","image":"images\/accessories\/accessories\/moqueta_grand-vitara-sz.jpg","description":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"13","created_at":"2017-07-25 13:25:08","modified_at":"2017-07-25 13:25:08","accessories_categories_id":"12"},"14":{"id":"14","title":"Kit Neblinero","slug":"kit-neblinero","code":"990E0-65J36-0","price":"320.00","image":"images\/accessories\/accessories\/kit-neblinero_grand-vitara-sz.jpg","description":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"14","created_at":"2017-07-25 13:25:36","modified_at":"2017-07-25 13:25:36","accessories_categories_id":"12"}},"combos":[{"id":"10","title":"Combo 1","slug":"combo-1","intro_text":null,"intro_image":null,"full_text":"<p>Dolore excepteur sint quis ea in nisi laborum reprehenderit ea est laborum veniam commodo ut velit proident anim sed dolore eu officia aute ut culpa elit exercitation.<\/p> <p>Lorem ipsum ex id amet ad proident cupidatat ex anim aliquip est adipisicing cupidatat quis sed voluptate ex minim tempor ut anim ullamco magna reprehenderit dolor qui sint ullamco in fugiat sunt ut et aute duis et fugiat labore duis culpa minim enim consectetur quis amet aliqua culpa laborum officia labore cupidatat nostrud pariatur dolor quis ut mollit dolor sunt cillum veniam culpa nostrud eiusmod labore non eu amet minim esse aliquip tempor quis dolor velit occaecat excepteur qui sit aute mollit ea consectetur cillum tempor consequat incididunt irure ex dolore et dolor ea excepteur officia magna ut quis amet aute velit aliqua dolor reprehenderit anim sint excepteur mollit velit sit anim dolor occaecat eiusmod dolore voluptate aliquip est culpa ea labore officia consectetur aliquip non in cupidatat in sed esse anim velit ea fugiat in dolore in.<\/p>","full_image":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"1","created_at":"2017-07-19 11:48:45","modified_at":"2017-07-25 12:25:04","meta_title":"","meta_keywords":"","meta_description":"","accessories_categories_id":"12"},{"id":"14","title":"Combo Interiores","slug":"combo-interiores","intro_text":null,"intro_image":null,"full_text":"<p>lorem ipsun et cutae finalite sterious neomateus<\/p>","full_image":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"2","created_at":"2017-07-20 15:26:01","modified_at":"2017-07-25 13:26:57","meta_title":"","meta_keywords":"","meta_description":"","accessories_categories_id":"12"},{"id":"11","title":"Combo Completo","slug":"combo-completo","intro_text":null,"intro_image":null,"full_text":"","full_image":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"3","created_at":"2017-07-20 15:16:00","modified_at":"2017-07-25 13:27:33","meta_title":"","meta_keywords":"","meta_description":"","accessories_categories_id":"12"},{"id":"15","title":"Combo Interior","slug":"combo-interior","intro_text":null,"intro_image":null,"full_text":"","full_image":null,"level":"1","publish":"1","active":"0","trash":"0","archived":"0","row_order":"4","created_at":"2017-07-21 16:51:39","modified_at":"2017-07-25 13:28:04","meta_title":"","meta_keywords":"","meta_description":"","accessories_categories_id":"12"}],"comboItems":[{"accessories_id":"11","combos_id":"14","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 13:26:48","modified_at":"2017-07-25 13:26:48"},{"accessories_id":"9","combos_id":"11","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 13:27:30","modified_at":"2017-07-25 13:27:30"},{"accessories_id":"14","combos_id":"10","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-08-09 09:39:29","modified_at":"2017-08-09 09:39:29"},{"accessories_id":"14","combos_id":"11","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 13:27:27","modified_at":"2017-07-25 13:27:27"},{"accessories_id":"9","combos_id":"15","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 13:27:52","modified_at":"2017-07-25 13:27:52"},{"accessories_id":"10","combos_id":"11","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 13:27:30","modified_at":"2017-07-25 13:27:30"},{"accessories_id":"10","combos_id":"10","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-27 14:00:50","modified_at":"2017-07-27 14:00:50"},{"accessories_id":"12","combos_id":"14","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 13:26:50","modified_at":"2017-07-25 13:26:50"},{"accessories_id":"13","combos_id":"15","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 13:27:54","modified_at":"2017-07-25 13:27:54"},{"accessories_id":"11","combos_id":"11","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 13:27:29","modified_at":"2017-07-25 13:27:29"},{"accessories_id":"6","combos_id":"10","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 14:59:10","modified_at":"2017-07-25 14:59:10"},{"accessories_id":"13","combos_id":"11","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 13:27:27","modified_at":"2017-07-25 13:27:27"},{"accessories_id":"8","combos_id":"11","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 13:27:31","modified_at":"2017-07-25 13:27:31"},{"accessories_id":"11","combos_id":"10","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-08-18 09:43:05","modified_at":"2017-08-18 09:43:05"},{"accessories_id":"13","combos_id":"10","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-08-09 09:39:28","modified_at":"2017-08-09 09:39:28"},{"accessories_id":"6","combos_id":"11","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 13:27:25","modified_at":"2017-07-25 13:27:25"},{"accessories_id":"12","combos_id":"11","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 13:27:28","modified_at":"2017-07-25 13:27:28"},{"accessories_id":"9","combos_id":"10","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-26 11:23:25","modified_at":"2017-07-26 11:23:25"},{"accessories_id":"7","combos_id":"11","level":"1","publish":"0","active":"0","trash":"0","archived":"0","row_order":null,"created_at":"2017-07-25 13:27:26","modified_at":"2017-07-25 13:27:26"}],"combosPerCategory":{"12":{"num_combos":"4","accessories_categories_id":"12"}}

    El problema me presenta cuando yo envio la informacion, en el caso particular de accesories, no se como puedo generar la informacion de esta manera:

    "accessories": {
        "6": {
          "id": "6",
          "title": "Molduras Laterales",
          "slug": "molduras-laterales",
          "code": "990E0-65J07",
          "price": "288.00",
          "image": "images\/accessories\/accessories\/molduras-laterales_grand-vitara-sz.jpg",
          "description": null,
          "level": "1",
          "publish": "1",
          "active": "0",
          "trash": "0",
          "archived": "0",
          "row_order": "6",
          "created_at": "2017-07-25 12:24:13",
          "modified_at": "2017-07-25 12:24:13",
          "accessories_categories_id": "12"
        },
        "7": {
          "id": "7",
          "title": "Salpicadero para llamanta",
          "slug": "salpicadero-para-llamanta",
          "code": "990E0-65J64",
          "price": "72.08",
          "image": "images\/accessories\/accessories\/salpicadero-para-llamanta_grand-vitara-sz.jpg",
          "description": null,
          "level": "1",
          "publish": "1",
          "active": "0",
          "trash": "0",
          "archived": "0",
          "row_order": "7",
          "created_at": "2017-07-25 12:29:04",
          "modified_at": "2017-07-25 13:19:57",
          "accessories_categories_id": "12"
        },
        "8": {
          "id": "8",
          "title": "Barra útilitaria de techo",
          "slug": "barra-utilitaria-de-techo",
          "code": "990E0-65J18",
          "price": "346.81",
          "image": "images\/accessories\/accessories\/barra-utilitaria-de-techo_grand-vitara-sz.jpg",
          "description": null,
          "level": "1",
          "publish": "1",
          "active": "0",
          "trash": "0",
          "archived": "0",
          "row_order": "8",
          "created_at": "2017-07-25 13:20:43",
          "modified_at": "2017-07-25 13:20:43",
          "accessories_categories_id": "12"
        },
        "9": {
          "id": "9",
          "title": "Bandera de Asiento",
          "slug": "bandera-de-asiento",
          "code": "99000-990N8-1",
          "price": "53.33",
          "image": "images\/accessories\/accessories\/bandera-de-asiento_grand-vitara-sz.jpg",
          "description": null,
          "level": "1",
          "publish": "1",
          "active": "0",
          "trash": "0",
          "archived": "0",
          "row_order": "9",
          "created_at": "2017-07-25 13:22:14",
          "modified_at": "2017-07-25 13:22:36",
          "accessories_categories_id": "12"
        },
        "10": {
          "id": "10",
          "title": "Candado Plástico",
          "slug": "candado-plastico",
          "code": "99000-99037-2",
          "price": "8.47",
          "image": "images\/accessories\/accessories\/candado-plastico_grand-vitara-sz.jpg",
          "description": null,
          "level": "1",
          "publish": "1",
          "active": "0",
          "trash": "0",
          "archived": "0",
          "row_order": "10",
          "created_at": "2017-07-25 13:23:15",
          "modified_at": "2017-07-25 13:23:15",
          "accessories_categories_id": "12"
        },
        "11": {
          "id": "11",
          "title": "Tuercas de Seguridad",
          "slug": "tuercas-de-seguridad",
          "code": "990E0-58-J49",
          "price": "58.97",
          "image": "images\/accessories\/accessories\/tuercas-de-seguridad_grand-vitara-sz.jpg",
          "description": null,
          "level": "1",
          "publish": "1",
          "active": "0",
          "trash": "0",
          "archived": "0",
          "row_order": "11",
          "created_at": "2017-07-25 13:23:38",
          "modified_at": "2017-07-25 13:23:47",
          "accessories_categories_id": "12"
        },
        "12": {
          "id": "12",
          "title": "Tubo Cromado",
          "slug": "tubo-cromado",
          "code": "99000-99069-3",
          "price": "129.72",
          "image": "images\/accessories\/accessories\/tubo-cromado_grand-vitara-sz.jpg",
          "description": null,
          "level": "1",
          "publish": "1",
          "active": "0",
          "trash": "0",
          "archived": "0",
          "row_order": "12",
          "created_at": "2017-07-25 13:24:28",
          "modified_at": "2017-07-25 13:24:28",
          "accessories_categories_id": "12"
        },
        "13": {
          "id": "13",
          "title": "Moqueta",
          "slug": "moqueta",
          "code": "99000-99037-6",
          "price": "43.50",
          "image": "images\/accessories\/accessories\/moqueta_grand-vitara-sz.jpg",
          "description": null,
          "level": "1",
          "publish": "1",
          "active": "0",
          "trash": "0",
          "archived": "0",
          "row_order": "13",
          "created_at": "2017-07-25 13:25:08",
          "modified_at": "2017-07-25 13:25:08",
          "accessories_categories_id": "12"
        },
        "14": {
          "id": "14",
          "title": "Kit Neblinero",
          "slug": "kit-neblinero",
          "code": "990E0-65J36-0",
          "price": "320.00",
          "image": "images\/accessories\/accessories\/kit-neblinero_grand-vitara-sz.jpg",
          "description": null,
          "level": "1",
          "publish": "1",
          "active": "0",
          "trash": "0",
          "archived": "0",
          "row_order": "14",
          "created_at": "2017-07-25 13:25:36",
          "modified_at": "2017-07-25 13:25:36",
          "accessories_categories_id": "12"
        }
      },


    Esta es mi clase:

    public class objetoConsultaGeneral : EntidadBase
        {
            public categories[] categories { get; set; }
            public accessories[] accesories { get; set; }
            public combos[] combos { get; set; }
            public comboItems[] comboItems { get; set; }
        }

    Y este es mi resultado

    {"accesories":[{"accessories_categories_id":"7","active":"0","archived":"0","code":"93742333","created_at":"2007-02-06T00:00:00","description":null,"id":"40556","image":"https://apps.autoconsa.com/Publicacion.Servicio.Imagen.Autoconsa/93742333.jpg","level":"1","modified_at":"2007-02-06T15:50:31.857","price":"18.42","publish":"1","row_order":"40556","slug":"93742333","title":"KIT SALPICADERA 4 P AVEO","trash":"0"},{"accessories_categories_id":"7","active":"0","archived":"0","code":"VE006","created_at":"2007-05-31T00:00:00","description":null,"id":"41516","image":"https://apps.autoconsa.com/Publicacion.Servicio.Imagen.Autoconsa/imagen-no-disponible.gif","level":"1","modified_at":"2007-05-31T12:41:21.687","price":"237","publish":"1","row_order":"41516","slug":"VE006","title":"ALZA CRISTALES 4P","trash":"0"},{"accessories_categories_id":"7","active":"0","archived":"0","code":"95631051","created_at":"2009-09-05T00:00:00","description":null,"id":"48328","image":"https://apps.autoconsa.com/Publicacion.Servicio.Imagen.Autoconsa/95631051.jpg","level":"1","modified_at":"2009-09-05T11:56:25.72","price":"860.82","publish":"1","row_order":"48328","slug":"95631051","title":"AIRE ACONDICIONADO AVEO","trash":"0"},{"accessories_categories_id":"7","active":"0","archived":"0","code":"95631084","created_at":"2010-09-08T00:00:00","description":null,"id":"51245","image":"https://apps.autoconsa.com/Publicacion.Servicio.Imagen.Autoconsa/imagen-no-disponible.gif","level":"1","modified_at":"2010-09-08T12:05:01.8","price":"74.72","publish":"1","row_order":"51245","slug":"95631084","title":"PROTECTOR DEL CARTER FAMILIA AVEO PLASTICO","trash":"0"},{"accessories_categories_id":"7","active":"0","archived":"0","code":"1126590","created_at":"1900-01-01T00:00:00","description":null,"id":"64245","image":"https://apps.autoconsa.com/Publicacion.Servicio.Imagen.Autoconsa/1126590.jpg","level":"1","modified_at":"2015-03-06T15:31:18.687","price":"160.7","publish":"1","row_order":"64245","slug":"1126590","title":"LAMINA DE SEGURIDAD 8 MIL GM TODO VEHICULO","trash":"0"}],"categories":[{"active":"0","archived":"0","created_at":"2006-07-21 04:07:12","full_image":null,"full_text":"","id":"7","intro_image":null,"intro_text":null,"level":"1","meta_description":"","meta_keywords":"","meta_title":"","modified_at":"2006-07-21 04:07:12","publish":"1","row_order":"1","slug":"aveo","title":"AVEO","trash":"0","vehicles_id":"0"}],"comboItems":[{"accessories_id":51245.0,"active":"0","archived":"0","combos_id":325.0,"created_at":"2010-09-08T00:00:00","level":"1","modified_at":"2010-09-08T12:05:01.8","publish":"1","row_order":null,"trash":"0"},{"accessories_id":48328.0,"active":"0","archived":"0","combos_id":325.0,"created_at":"2009-09-05T00:00:00","level":"1","modified_at":"2009-09-05T11:56:25.72","publish":"1","row_order":null,"trash":"0"},{"accessories_id":40556.0,"active":"0","archived":"0","combos_id":325.0,"created_at":"2007-02-06T00:00:00","level":"1","modified_at":"2007-02-06T15:50:31.857","publish":"1","row_order":null,"trash":"0"},{"accessories_id":48328.0,"active":"0","archived":"0","combos_id":326.0,"created_at":"2009-09-05T00:00:00","level":"1","modified_at":"2009-09-05T11:56:25.72","publish":"1","row_order":null,"trash":"0"},{"accessories_id":64245.0,"active":"0","archived":"0","combos_id":326.0,"created_at":"1900-01-01T00:00:00","level":"1","modified_at":"2015-03-06T15:31:18.687","publish":"1","row_order":null,"trash":"0"},{"accessories_id":51245.0,"active":"0","archived":"0","combos_id":326.0,"created_at":"2010-09-08T00:00:00","level":"1","modified_at":"2010-09-08T12:05:01.8","publish":"1","row_order":null,"trash":"0"},{"accessories_id":41516.0,"active":"0","archived":"0","combos_id":327.0,"created_at":"2007-05-31T00:00:00","level":"1","modified_at":"2007-05-31T12:41:21.687","publish":"1","row_order":null,"trash":"0"},{"accessories_id":48328.0,"active":"0","archived":"0","combos_id":327.0,"created_at":"2009-09-05T00:00:00","level":"1","modified_at":"2009-09-05T11:56:25.72","publish":"1","row_order":null,"trash":"0"},{"accessories_id":41516.0,"active":"0","archived":"0","combos_id":328.0,"created_at":"2007-05-31T00:00:00","level":"1","modified_at":"2007-05-31T12:41:21.687","publish":"1","row_order":null,"trash":"0"},{"accessories_id":48328.0,"active":"0","archived":"0","combos_id":328.0,"created_at":"2009-09-05T00:00:00","level":"1","modified_at":"2009-09-05T11:56:25.72","publish":"1","row_order":null,"trash":"0"},{"accessories_id":64245.0,"active":"0","archived":"0","combos_id":328.0,"created_at":"1900-01-01T00:00:00","level":"1","modified_at":"2015-03-06T15:31:18.687","publish":"1","row_order":null,"trash":"0"},{"accessories_id":41516.0,"active":"0","archived":"0","combos_id":329.0,"created_at":"2007-05-31T00:00:00","level":"1","modified_at":"2007-05-31T12:41:21.687","publish":"1","row_order":null,"trash":"0"},{"accessories_id":48328.0,"active":"0","archived":"0","combos_id":329.0,"created_at":"2009-09-05T00:00:00","level":"1","modified_at":"2009-09-05T11:56:25.72","publish":"1","row_order":null,"trash":"0"},{"accessories_id":64245.0,"active":"0","archived":"0","combos_id":329.0,"created_at":"1900-01-01T00:00:00","level":"1","modified_at":"2015-03-06T15:31:18.687","publish":"1","row_order":null,"trash":"0"}],"combos":[{"accessories_categories_id":"7","active":"0","archived":"0","created_at":"2017-08-03T09:32:52.27","full_image":null,"full_text":"COMBO AVEO FAMILY 1","id":325.0,"intro_image":null,"intro_text":null,"level":"1","meta_description":"","meta_keywords":"","meta_title":"","modified_at":"2017-08-03T09:32:52.27","publish":"1","row_order":"1","slug":"ACC-3","title":"COMBO AVEO FAMILY 1","trash":"0"},{"accessories_categories_id":"7","active":"0","archived":"0","created_at":"2017-08-03T09:35:27.36","full_image":null,"full_text":"COMBO AVEO FAMILY 2","id":326.0,"intro_image":null,"intro_text":null,"level":"1","meta_description":"","meta_keywords":"","meta_title":"","modified_at":"2017-08-03T09:35:27.36","publish":"1","row_order":"1","slug":"ACC-4","title":"COMBO AVEO FAMILY 2","trash":"0"},{"accessories_categories_id":"7","active":"0","archived":"0","created_at":"2017-08-03T09:37:33.203","full_image":null,"full_text":"COMBO AVEO FAMILY 3","id":327.0,"intro_image":null,"intro_text":null,"level":"1","meta_description":"","meta_keywords":"","meta_title":"","modified_at":"2017-08-03T09:37:33.203","publish":"1","row_order":"1","slug":"ACC-5","title":"COMBO AVEO FAMILY 3","trash":"0"},{"accessories_categories_id":"7","active":"0","archived":"0","created_at":"2017-08-03T09:39:37.263","full_image":null,"full_text":"COMBO AVEO FAMILY 3","id":328.0,"intro_image":null,"intro_text":null,"level":"1","meta_description":"","meta_keywords":"","meta_title":"","modified_at":"2017-08-03T09:39:37.263","publish":"1","row_order":"1","slug":"ACC-6","title":"COMBO AVEO FAMILY 3","trash":"0"},{"accessories_categories_id":"7","active":"0","archived":"0","created_at":"2017-08-03T09:43:12.107","full_image":null,"full_text":"COMBO AVEO FAMILY 4","id":329.0,"intro_image":null,"intro_text":null,"level":"1","meta_description":"","meta_keywords":"","meta_title":"","modified_at":"2017-08-03T09:43:12.107","publish":"1","row_order":"1","slug":"ACC-7","title":"COMBO AVEO FAMILY 4","trash":"0"},{"accessories_categories_id":"7","active":"0","archived":"0","created_at":"2017-08-04T16:15:55.02","full_image":null,"full_text":"PINTURA TAXI","id":334.0,"intro_image":null,"intro_text":null,"level":"1","meta_description":"","meta_keywords":"","meta_title":"","modified_at":"2017-08-04T16:15:55.02","publish":"1","row_order":"1","slug":"ACC-12","title":"PINTURA TAXI","trash":"0"}]}

    Los accesorios el cual es mi problema:

    "accesories": [
        {
          "accessories_categories_id": "7",
          "active": "0",
          "archived": "0",
          "code": "93742333",
          "created_at": "2007-02-06T00:00:00",
          "description": null,
          "id": "40556",
          "image": "https://apps.autoconsa.com/Publicacion.Servicio.Imagen.Autoconsa/93742333.jpg",
          "level": "1",
          "modified_at": "2007-02-06T15:50:31.857",
          "price": "18.42",
          "publish": "1",
          "row_order": "40556",
          "slug": "93742333",
          "title": "KIT SALPICADERA 4 P AVEO",
          "trash": "0"
        },
        {
          "accessories_categories_id": "7",
          "active": "0",
          "archived": "0",
          "code": "VE006",
          "created_at": "2007-05-31T00:00:00",
          "description": null,
          "id": "41516",
          "image": "https://apps.autoconsa.com/Publicacion.Servicio.Imagen.Autoconsa/imagen-no-disponible.gif",
          "level": "1",
          "modified_at": "2007-05-31T12:41:21.687",
          "price": "237",
          "publish": "1",
          "row_order": "41516",
          "slug": "VE006",
          "title": "ALZA CRISTALES 4P",
          "trash": "0"
        },
        {
          "accessories_categories_id": "7",
          "active": "0",
          "archived": "0",
          "code": "95631051",
          "created_at": "2009-09-05T00:00:00",
          "description": null,
          "id": "48328",
          "image": "https://apps.autoconsa.com/Publicacion.Servicio.Imagen.Autoconsa/95631051.jpg",
          "level": "1",
          "modified_at": "2009-09-05T11:56:25.72",
          "price": "860.82",
          "publish": "1",
          "row_order": "48328",
          "slug": "95631051",
          "title": "AIRE ACONDICIONADO AVEO",
          "trash": "0"
        },
        {
          "accessories_categories_id": "7",
          "active": "0",
          "archived": "0",
          "code": "95631084",
          "created_at": "2010-09-08T00:00:00",
          "description": null,
          "id": "51245",
          "image": "https://apps.autoconsa.com/Publicacion.Servicio.Imagen.Autoconsa/imagen-no-disponible.gif",
          "level": "1",
          "modified_at": "2010-09-08T12:05:01.8",
          "price": "74.72",
          "publish": "1",
          "row_order": "51245",
          "slug": "95631084",
          "title": "PROTECTOR DEL CARTER FAMILIA AVEO PLASTICO",
          "trash": "0"
        },
        {
          "accessories_categories_id": "7",
          "active": "0",
          "archived": "0",
          "code": "1126590",
          "created_at": "1900-01-01T00:00:00",
          "description": null,
          "id": "64245",
          "image": "https://apps.autoconsa.com/Publicacion.Servicio.Imagen.Autoconsa/1126590.jpg",
          "level": "1",
          "modified_at": "2015-03-06T15:31:18.687",
          "price": "160.7",
          "publish": "1",
          "row_order": "64245",
          "slug": "1126590",
          "title": "LAMINA DE SEGURIDAD 8 MIL GM TODO VEHICULO",
          "trash": "0"
        }
      ],

    Si alguien me puede explicar que estoy haciendo mal o como lo puedo corregir, les agradeceria un mundo


    Vladimir Miranda Desarrollador Jr.

    jueves, 28 de septiembre de 2017 14:23

Respuestas

  • ¡Ugh! Es bastante feo. Tu estás usando una colección de tu clase, y al serializarlo arroja un array de object, cosa que es bastante razonable. Pero el modelo que te han pedido es un objeto que dentro tiene propiedades cuyos nombres son valores numéricos y sus valores son objetos. Me temo que no vas a poder hacer esto desde C# serializando una clase, salvo que hagas cosas raras mediante generación dinámica de código. No creo que valga la pena recurrir a semejantes complicaciones únicamente para que funcione el serializarod JSON. Seguramente te quedará mas simple si generas el JSON mediante un bucle concatenando el texto para formar el formato deseado. Cada una de las instancias de la clase sí que puedes serializarla con el serializador JSON, pero a la hora de "juntarlas" para dar lugar al objeto externo, esta es la parte que te quedará más sencilla concatenando texto mediante un StringBuilder en lugar de usando un serializador JSON.
    jueves, 28 de septiembre de 2017 15:51
  • hola

    cuando usan numeros en el json cada uno de estos seria una propiedad, puedes ayudarte con la tool

    http://json2csharp.com/

    public class Root{
    
        public  Accesorio accessories {get;set;}
    
    }
    
    public class Accesorio{
    
        public dato 6 {get;set;}
        public dato 7 {get;set;}
        public dato 8 {get;set;}
    
       //resto
    
    }
    
    public class dato {
    
        public string id {get;set;}
    
          public string title {get;set;}
    
         //resto propiedades
    
    }


    como veras hay propiedades y no una lista d

    saludos


    Leandro Tuttini

    Blog
    MVP Profile
    Buenos Aires
    Argentina

    jueves, 28 de septiembre de 2017 16:57

Todas las respuestas

  • ¡Ugh! Es bastante feo. Tu estás usando una colección de tu clase, y al serializarlo arroja un array de object, cosa que es bastante razonable. Pero el modelo que te han pedido es un objeto que dentro tiene propiedades cuyos nombres son valores numéricos y sus valores son objetos. Me temo que no vas a poder hacer esto desde C# serializando una clase, salvo que hagas cosas raras mediante generación dinámica de código. No creo que valga la pena recurrir a semejantes complicaciones únicamente para que funcione el serializarod JSON. Seguramente te quedará mas simple si generas el JSON mediante un bucle concatenando el texto para formar el formato deseado. Cada una de las instancias de la clase sí que puedes serializarla con el serializador JSON, pero a la hora de "juntarlas" para dar lugar al objeto externo, esta es la parte que te quedará más sencilla concatenando texto mediante un StringBuilder en lugar de usando un serializador JSON.
    jueves, 28 de septiembre de 2017 15:51
  • hola

    cuando usan numeros en el json cada uno de estos seria una propiedad, puedes ayudarte con la tool

    http://json2csharp.com/

    public class Root{
    
        public  Accesorio accessories {get;set;}
    
    }
    
    public class Accesorio{
    
        public dato 6 {get;set;}
        public dato 7 {get;set;}
        public dato 8 {get;set;}
    
       //resto
    
    }
    
    public class dato {
    
        public string id {get;set;}
    
          public string title {get;set;}
    
         //resto propiedades
    
    }


    como veras hay propiedades y no una lista d

    saludos


    Leandro Tuttini

    Blog
    MVP Profile
    Buenos Aires
    Argentina

    jueves, 28 de septiembre de 2017 16:57
  • Es curioso que el mensaje anterior haya sido marcado como respuesta, porque aunque conceptualmente es correcto, en la práctica no se puede usar. El principal motivo es que las propiedades no pueden tener nombres tales como "6", "7" y "8". Se produce un error al compilar porque el compilador exige que los identificadores empiecen por una letra o por "_", no pueden comenzar por un número.

    La segunda razón es que haciéndolo así el número de entradas estaría siempre fijo, es decir, no puedes ir generando registros de 1 a N donde la N es distinta cada vez.

    sábado, 30 de septiembre de 2017 8:01
  • El problema con tu solución es que no puedo tener una propiedad "6" porque no es un nombre aceptado. Despues, necesito que esa propiedad "6" en un tiempo ya no exista y tenga otra propiedad "10" y despues una "n" propiedad.

    Vladimir Miranda Desarrollador Jr.

    viernes, 6 de octubre de 2017 18:24
  • Al final considere tu respuesta y decidí realizar esto:

    PR.objetoConsultaGeneral objetoConsulta = consulta.ConsultarCategoriaAccesorios(id, 1);
                        cantidad_accesorios = objetoConsulta.accesories.Count();
                        for (int i = 0; i < cantidad_accesorios; i++)
                        {
                            accesorios += String.Format("'{0}':{{", objetoConsulta.accesories[i].id);
                            accesorios += String.Format(@"'id':'{0}',", objetoConsulta.accesories[i].id);
                            accesorios += String.Format(@"'title':'{0}',", objetoConsulta.accesories[i].title);
                            accesorios += String.Format(@"'slug':'{0}',", objetoConsulta.accesories[i].slug);
                            accesorios += String.Format(@"'code':'{0}',", objetoConsulta.accesories[i].code);
                            accesorios += String.Format(@"'price':'{0}',", objetoConsulta.accesories[i].price);
                            accesorios += String.Format(@"'image':'{0}',", objetoConsulta.accesories[i].image);
                            accesorios += String.Format(@"'description':'{0}',", objetoConsulta.accesories[i].description);
                            accesorios += String.Format(@"'level':'{0}',", objetoConsulta.accesories[i].level);
                            accesorios += String.Format(@"'publish':'{0}',", objetoConsulta.accesories[i].publish);
                            accesorios += String.Format(@"'active':'{0}',", objetoConsulta.accesories[i].active);
                            accesorios += String.Format(@"'trash':'{0}',", objetoConsulta.accesories[i].trash);
                            accesorios += String.Format(@"'archived':'{0}',", objetoConsulta.accesories[i].archived);
                            accesorios += String.Format(@"'row_order':'{0}',", objetoConsulta.accesories[i].row_order);
                            accesorios += String.Format(@"'created_at':'{0}',", objetoConsulta.accesories[i].created_at.ToString("yyyy-MM-dd  hh:mm:ss"));
                            accesorios += String.Format(@"'modified_at':'{0}',", objetoConsulta.accesories[i].modified_at.ToString("yyyy-MM-dd  hh:mm:ss"));
                            accesorios += String.Format(@"'accessories_categories_id':'{0}'", objetoConsulta.accesories[i].accessories_categories_id);
                            accesorios += String.Format(@"}},");
                        }
    
                        foreach (PR.categories cat in objetoConsulta.categories)
                        {
                            categorias += WriteFromObject(cat);
                            categorias += ",";
                            combosPorCategoria += String.Format("'{0}':{{", cat.id);
                            combosPorCategoria += String.Format("'num_combos':'4',");
                            combosPorCategoria += String.Format("'accessories_categories_id':'{0}'", cat.id);
                            combosPorCategoria += String.Format("}}");
                        }
    
                        foreach (PR.combos com in objetoConsulta.combos)
                        {
                            combos += WriteFromObject(com);
                            combos += ",";
                        }
    
                        foreach (PR.comboItems cmi in objetoConsulta.comboItems)
                        {
                            comboItems += WriteFromObject(cmi);
                            comboItems += ",";
                        }
    
    
                        categorias = categorias.Substring(0, categorias.LastIndexOf(","));
                        combos = combos.Substring(0, combos.LastIndexOf(","));
                        comboItems = comboItems.Substring(0, comboItems.LastIndexOf(","));
                        combosPorCategoria = String.Format("'combosPerCategory':{{{0}}}", combosPorCategoria);
                        categorias = String.Format("'categories':[{0}],", categorias);
                        accesorios = String.Format("'accessories':{{{0}}},", accesorios);
                        combos = String.Format("'combos':[{0}],", combos);
                        comboItems = String.Format("'comboItems':[{0}],", comboItems);
                        combosPorCategoria = combosPorCategoria.Replace("'", "\"");
                        categorias = categorias.Replace("'", "\"");
                        accesorios = accesorios.Replace("'", "\"");
                        combos = combos.Replace("'", "\"");
                        comboItems = comboItems.Replace("'", "\"");
    
                        JsonGeneral = String.Format("{{{0}{1}{2}{3}{4}}}", new object[] { categorias, accesorios, combos, comboItems, combosPorCategoria });
                        Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(JsonGeneral);
                        return json;

    Y asi entrego el formato solicitado :)


    Vladimir Miranda Desarrollador Jr.

    viernes, 6 de octubre de 2017 18:25
  • Y asi entrego el formato solicitado

    Si quieres un consejo para optimizarlo, utiliza un StringBuilder en lugar de ir concatenando en un string.

    El problema es que en .Net los strings son inmutables, por lo que cada vez que cambias el valor del string, es necesario asignar memoria para un string nuevo, copiar ahí el valor, y destruir la asignación de memoria del string antiguo. Esto ocurre cada vez que ejecutas una sentencia tal como accesorios+=algunaCosa. Si eso lo haces un montón de veces seguidas, y encima lo metes dentro de un bucle que lo repite muchas veces, resulta en una presión tremenda para el gestor de memoria. Por eso existe el StringBuilder, que sabe hacer las mismas operaciones usando un buffer para no andar creando y destruyendo objetos por cada una de las concatenaciones.

    viernes, 6 de octubre de 2017 20:23