0

Tengo el siguiente problema, recupero este json de una consulta con la propiedad SubItems y dentro de esta viene n cantidad de objetos nombrados con numeros en secuencia y necesito darle formato con un modelo pero no consigo encontrar una forma, hay que tener en cuenta que pueden ser n elementos dentro del objeto SubItems

SubItems\": {\"1\": {\"Name\": \"Aros de cebolla\", \"tax1\": {\"TAXID\": \"6\", \"TAXNAME\": \"IVA 12\", \"TAXRATE\": \"0.12\", \"TAXEXCLUSIVE\": \"N\", \"TAXINCLUSIVE\": \"Y\"}, \"tax2\": \"\", \"Price\": 0, \"Quantity\": 1, \"ModCodeId\": 1, \"MenuItemId\": 23116, \"ModGroupId\": 10067, \"PriceTotal\": 0, \"Quantity_inicial\": 1}, \"2\": {\"Name\": \"Coca-Cola Sabor Original 400cc\", \"tax1\": {\"TAXID\": \"6\", \"TAXNAME\": \"IVA 12\", \"TAXRATE\": \"0.12\", \"TAXEXCLUSIVE\": \"N\", \"TAXINCLUSIVE\": \"Y\"}, \"tax2\": \"\", \"Price\": 0, \"Quantity\": 1, \"ModCodeId\": 1, \"MenuItemId\": 25263, \"ModGroupId\": 10066, \"PriceTotal\": 0, \"Quantity_inicial\": 1}},

1 respuesta 1

0

Podrías hacer uso de Newtonsoft.Json y crear un método que le de formato a los json string:

   public class JsonFoo
   {
      public static string AplicarFormato(string jsonString)
      {
         dynamic json= JsonConvert.DeserializeObject(jsonString);
         return JsonConvert.SerializeObject(json, Formatting.Indented);
      }
   }

Con ese método, podrías obtener un json string con formato de la siguiente manera:

var jsonString = JsonFoo.AplicarFormato(objeto.SubItems);

Si lo escribes en un archivo json, se ve de la siguiente forma:

{
  "1": {
    "Name": "Aros de cebolla",
    "tax1": {
      "TAXID": "6",
      "TAXNAME": "IVA 12",
      "TAXRATE": "0.12",
      "TAXEXCLUSIVE": "N",
      "TAXINCLUSIVE": "Y"
    },
    "tax2": "",
    "Price": 0,
    "Quantity": 1,
    "ModCodeId": 1,
    "MenuItemId": 23116,
    "ModGroupId": 10067,
    "PriceTotal": 0,
    "Quantity_inicial": 1
  },
  "2": {
    "Name": "Coca-Cola Sabor Original 400cc",
    "tax1": {
      "TAXID": "6",
      "TAXNAME": "IVA 12",
      "TAXRATE": "0.12",
      "TAXEXCLUSIVE": "N",
      "TAXINCLUSIVE": "Y"
    },
    "tax2": "",
    "Price": 0,
    "Quantity": 1,
    "ModCodeId": 1,
    "MenuItemId": 25263,
    "ModGroupId": 10066,
    "PriceTotal": 0,
    "Quantity_inicial": 1
  }
}
2
  • Ya intente realizarlo como me mecionas y no me da el resultado esperado, de hecho me arroja un error de formato cuando intenta desearilzar. Es que como te comento esta propiedad es parte de un objeto que a su ves el objeto padre es una propiedades de un objeto aun mayor, y ya tengo el modelo de datos pero especificamente esta propiedad SubItems me arroja un error el 10 jul. 2020 a las 16:42
  • Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'AgenteWolop.Models.SubItems' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. el 10 jul. 2020 a las 16:43

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.