【问题标题】:Need help reading a Json Object List which is not inside brackets []需要帮助阅读不在括号 [] 内的 Json 对象列表
【发布时间】:2021-07-26 23:32:51
【问题描述】:

这是我尝试反序列化的 Json 部分的示例,我还需要能够通过 POST 发送类似的 Json。问题是我不确定我应该如何读取所有这些对象并将它们添加到特定的“adocumentitems”对象列表中,而不是将其序列化为 JSON 格式的那种类型的对象列表。

{
"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"
               }
}

我所知道的 JSON 列表的类型看起来更像这样:

{ "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"   
      }  ]
}

但事实并非如此……

我正在使用 C#,尝试对其进行反序列化并使用 NewtonSoft 对其进行序列化。

将对象序列化为 Json 以用于 POST 目的

var JsonData = JsonConvert.SerializeObject(adocumentitems);

在执行 GET 时反序列化对对象的 json 响应

 return JsonConvert.DeserializeObject<adocumentitems>(JsonGetResponse);

如果有人能帮助我了解如何处理这种类型的 json 列表,我将不胜感激。

【问题讨论】:

标签: c# arrays json api


【解决方案1】:

在 JSON 中 { } 是字典,而不是列表。

因此你必须把它当作字典来处理。

结论:这个 JSON 是一个字典,包含一个字典。

【讨论】:

    【解决方案2】:

    您的问题是您的 json 已损坏,adocumentitems 不是数组/列表而是字典,您需要将其声明为这样,或者如果您使用 System.Text,则需要使用基本的 josn 类。 Json 这些将是 JsonDocument、JsonProperty 和 JsonElement,您可以执行以下操作

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

    这将使您可以访问JsonDocument.RootElement,从而可以使用TryGetProperty(propName, out var prop)

    这可能看起来像

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

    其中 parseDictionaryAsList 被声明为

    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>();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      相关资源
      最近更新 更多