简体   繁体   中英

How to convert JSON string to an array

I have the following JSON string:

[
   {
      "name":"Test diagnosis",
      "code":"324324",
      "table":"EXAMPLE",
      "addedby":"EDnurse",
      "dateadded":3243243,
      "qualifier":[
         {
            "name":"Qualifier",
            "value":"Confirmed Diagnosis",
            "code":"23434434",
            "prefix":"[C] "
         },
         {
            "name":"Left/Right",
            "value":"Bilateral",
            "code":"324343",
            "suffix":" - Bilateral"
         }
      ],
      "prefix":"[C] ",
      "suffix":" - Bilateral"
   }
]

You can see that the Qualifier field in this JSON String is nested and has 2 objects.

I am working on a package that parses this JSON string using C# in SSIS. I can Parse the string with one object of qualifier, but when I add the second object (left/right), and attempt to turn the string into an array, I receive an error.

Without array (works with one Qualifier object):

Diagnosis diagnosis = js.Deserialize<Diagnosis>(reviewConverted);

With array (returns error stating that I cannot implicitly convert type diagnosis to type diagnosis ):

Diagnosis diagnosis = js.Deserialize<List<Diagnosis>>(reviewConverted);

I also use the following Class to define my diagnosis fields:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SC_8aae662509ae4bab8491323924579173
{
    class Diagnosis
    {
        public string name { get; set; }
        public string code { get; set; }
        public string table { get; set; }
        public string addedby { get; set; }
        public string dateadded { get; set; }

        public qualifier Qualifier { get; set; }

        public string prefix { get; set; }
        public string suffix { get; set; }


    }
}

Here is my qualifier class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SC_8aae662509ae4bab8491323924579173
{
    class qualifier
    {
        public string name { get; set; }
        public string value { get; set; }
        public string code { get; set; }
        public string prefix { get; set; }
    }
}

Did you try like below?

class Diagnosis
{
    public string name { get; set; }
    public string code { get; set; }
    public string table { get; set; }
    public string addedby { get; set; }
    public string dateadded { get; set; }

    public List<Qualifier> qualifier { get; set; }

    public string prefix { get; set; }
    public string suffix { get; set; }


}

As far as i can tell, based on what you're saying, 1 Diagnosis object, can contain multiple Qualifier objects. So what you need to do is the following: First, change your Diagnosis class to have the following property:

public qualifier List<Qualifier> { get; set; }

instead of

public qualifier Qualifier { get; set; }

Also the following statement is what gives you an error:

Diagnosis diagnosis = js.Deserialize<List<Diagnosis>>(reviewConverted);

You are trying to store in a Diagnosis objext a List of Diagnosis object, which makes no sense of course.

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