简体   繁体   中英

Need help reading a Json Object List which is not inside brackets []

This is an example of the part of the Json Im trying to deserialize,I also need to be able to send a similar Json by a POST. The thing is that im not sure how im suppose to read all those objects and add them to spesific "adocumentitems" object list, nether to serialize it to that type of object list in JSON format.

{
"adocumentitems":{
               "0":{
                  "sdescription":"PORTÁTILES SIN FLUOROSCOPIA E INTENSIFICADOR DE IMÁGENES PRACTICADO EN HABITACION UCI RN O QUIRÓFANOS",
                  "sstandarditemidentification":"21601_03 - 21601",
                  "nunitprice":21854,
                  "ntotal":21854,
                  "nquantity":1,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49             Fecha Egr: 2021-04-11 22:03:00        AUTORIZACION: 146693312"
               },
               "1":{
                  "sdescription":"RADIOGRAFIA DE TORAX P.A.O A.P.Y LATERAL DECUBITO LATERAL OBLICUAS O LATERAL CON BARIO",
                  "sstandarditemidentification":"871121_03 - 871121",
                  "nunitprice":28924,
                  "ntotal":28924,
                  "nquantity":1,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49             Fecha Egr: 2021-04-11 22:03:00        AUTORIZACION: 146693312"
               },
               "2":{
                  "sdescription":"TOMOGRAFIA AXIAL COMPUTADA DE TORAX",
                  "sstandarditemidentification":"879301_03 - 879301",
                  "nunitprice":141386,
                  "ntotal":282772,
                  "nquantity":2,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49             Fecha Egr: 2021-04-11 22:03:00        AUTORIZACION: 146693312"
               },
               "3":{
                  "sdescription":"CONSULTA DE PRIMERA VEZ POR ESPECIALISTA EN MEDICINA INTERNA",
                  "sstandarditemidentification":"890266_03 - 890266",
                  "nunitprice":17514,
                  "ntotal":17514,
                  "nquantity":1,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49             Fecha Egr: 2021-04-11 22:03:00        AUTORIZACION: 146693312"
               },
               "4":{
                  "sdescription":"CUIDADO MANEJO INTRAHOSPITALARIO POR MEDICINA ESPECIALIZADA",
                  "sstandarditemidentification":"890602_03 - 890602",
                  "nunitprice":21133,
                  "ntotal":42266,
                  "nquantity":2,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49             Fecha Egr: 2021-04-11 22:03:00        AUTORIZACION: 146693312"
               },
               "5":{
                  "sdescription":"CONSULTA DE URGENCIAS POR MEDICINA GENERAL",
                  "sstandarditemidentification":"890701_03 - 890701",
                  "nunitprice":17395,
                  "ntotal":17395,
                  "nquantity":1,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49             Fecha Egr: 2021-04-11 22:03:00        AUTORIZACION: 146693312"
               },
               "6":{
                  "sdescription":"HEMOCULTIVO AEROBIO POR METODO AUTOMATICO",
                  "sstandarditemidentification":"901221_03 - 901221",
                  "nunitprice":42560,
                  "ntotal":85120,
                  "nquantity":2,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49             Fecha Egr: 2021-04-11 22:03:00        AUTORIZACION: 146693312"
               },
               "7":{
                  "sdescription":"HEMOGRAMA IV [HEMOGLOBINA HEMATOCRITO RECUENTO DE ERITROCITOS-INDICES ERITROCITARIOS-LEUCOGRAMA-RECUENTO DE PLAQUETAS-INDICES PLAQUETARIOS Y MORFOLOGIA ELECTRONICA E HISTOGRAMA] METODO AUTOMATICO",
                  "sstandarditemidentification":"902210_03 - 902210",
                  "nunitprice":15421,
                  "ntotal":15421,
                  "nquantity":1,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49             Fecha Egr: 2021-04-11 22:03:00        AUTORIZACION: 146693312"
               },
               "8":{
                  "sdescription":"GASES ARTERIALES EN REPOSO O EN EJERCICIO",
                  "sstandarditemidentification":"903839_03 - 903839",
                  "nunitprice":17766,
                  "ntotal":35532,
                  "nquantity":2,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49             Fecha Egr: 2021-04-11 22:03:00        AUTORIZACION: 146693312"
               },
               "9":{
                  "sdescription":"HABITACION DE CUATRO CAMAS O MAS",
                  "sstandarditemidentification":"10A004_03 - S11304",
                  "nunitprice":91966,
                  "ntotal":183932,
                  "nquantity":2,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49             Fecha Egr: 2021-04-11 22:03:00        AUTORIZACION: 146693312"
               },
               "10":{
                  "sdescription":"OXIGENO CATETER O CANULA NASAL X HORA",
                  "sstandarditemidentification":"S55201_03 - S55201",
                  "nunitprice":2940,
                  "ntotal":152880,
                  "nquantity":52,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49             Fecha Egr: 2021-04-11 22:03:00        AUTORIZACION: 146693312"
               },
               "11":{
                  "sdescription":"ACIDO ACETIL SALICILICO  100 mg TABLETA",
                  "sstandarditemidentification":"19936296-08",
                  "nunitprice":100,
                  "ntotal":200,
                  "nquantity":2,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49             Fecha Egr: 2021-04-11 22:03:00        AUTORIZACION: 146693312"
               }
}

The type of JSON list that I know of would look more like this:

{ "adocumentitems": [
  {
          "sdescription":"PORTÁTILES SIN FLUOROSCOPIA E INTENSIFICADOR DE IMÁGENES PRACTICADO EN HABITACION UCI RN O QUIRÓFANOS",
                  "sstandarditemidentification":"21601_03 - 21601",
                  "nunitprice":21854,
                  "ntotal":21854,
                  "nquantity":1,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49"   
      },
   {
          "sdescription":"PORTÁTILES SIN FLUOROSCOPIA E INTENSIFICADOR DE IMÁGENES PRACTICADO EN HABITACION UCI RN O QUIRÓFANOS",
                  "sstandarditemidentification":"21601_03 - 21601",
                  "nunitprice":21854,
                  "ntotal":21854,
                  "nquantity":1,
                  "sgroupname":"TIPO DE INGRESO: URGENCIAS                 Fecha Ing: 2021-04-09 15:27:49"   
      }  ]
}

But its not this case...

Im working with C#, trying to deserialize it and serialize it with NewtonSoft.

Serializing Object to Json for POST purpose

var JsonData = JsonConvert.SerializeObject(adocumentitems);

Deserializing json response to object when performing a GET

 return JsonConvert.DeserializeObject<adocumentitems>(JsonGetResponse);

I'll appreciate if someone could help me understand how to handel this types of json list.

In JSON { } is a Dictionary, not a List<>.

Thus you have to handle it as a dictionary.

In conclusion: this JSON is a dictionary, containing a dictionary.

your issue is that your json is corrupted, adocumentitems is not a array/list but a dictionary, you either need to declare it as such or you need to work with the base josn classes if you are working with System.Text.Json these would be JsonDocument, JsonProperty and JsonElement and you can do the following

var document = JsonDocument.Parse(reader.ReadToEnd());

this would then give you access to the JsonDocument.RootElement , which lets you use TryGetProperty(propName, out var prop)

this might looks something like

JsonDocument.RootElement.parseDictionaryAsList("adocumentitems",v=>new aDocumentItem(v));

where parseDictionaryAsList is declared as

public static IEnumerable<TVal> parseDictionaryAsList<TVal>(this JsonElement json, string propName, Func<JsonElement, TVal> value)
    => json.TryGetProperty(propName, out var prop)
        ? prop.EnumerateObject().Select(v => value(v.Value))
        : Enumerable.Empty<TVal>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM