简体   繁体   中英

c# Serialize multiple nested objects to json

I have a need to serialize this from Objects, or possibly a object of objects in c#. I have tried multiple different ways to get the same output and have failed. Here is the JSON

    {
   "resourceType": "Observation",
   "code": {
     "coding": [
       {
         "system": "http://",
         "code": "3637",
         "display": "Gene"
       }
     ],
     "text": "Dip"
   },
   "subject": {
     "reference": "Pat",
     "display": ""
   },
   "valueString": "*1/*1",
   "component": [
     {
       "code": {
         "coding": [
           {
             "system": "http://",
             "code": "",
             "display": "Gene"
           }
         ]
       },
       "valueCodeableConcept": {
         "coding": [
           {
             "system": "http://",
             "code": "",
             "display": "CYP"
           }
         ]
       }
     }
   ]
 }

i created the following objects from this. I tried to make a object containing them and then use Newtonsoft to serialize it but I can not get it right

here are the objects

     public class Rootobject
    {
        public string resourceType { get; set; }
        public Code code { get; set; }
        public Subject subject { get; set; }
        public string valueString { get; set; }
        public Component[] component { get; set; }
    }

    public class Code
    {
        public Coding[] coding { get; set; }
        public string text { get; set; }
    }

    public class Coding
    {
        public string system { get; set; }
        public string code { get; set; }
        public string display { get; set; }
    }

    public class Subject
    {
        public string reference { get; set; }
        public string display { get; set; }
    }

    public class Component
    {
        public Code1 code { get; set; }
        public Valuecodeableconcept valueCodeableConcept { get; set; }
    }

    public class Code1
    {
        public Coding1[] coding { get; set; }
    }

    public class Coding1
    {
        public string system { get; set; }
        public string code { get; set; }
        public string display { get; set; }
    }

    public class Valuecodeableconcept
    {
        public Coding2[] coding { get; set; }
    }

    public class Coding2
    {
        public string system { get; set; }
        public string code { get; set; }
        public string display { get; set; }
    }

As Sir Rufo pointed out(for completeness of answer)

using System;
using Newtonsoft.Json;                      
public class Program
{
    public static void Main()
    {
        var obj = CreateRootObject();
        var str = JsonConvert.SerializeObject( obj, Formatting.Indented );
        Console.WriteLine(str);
    }

    private static Rootobject CreateRootObject() 
    {
        var obj = new Rootobject() {
            resourceType = "Observation",
            code = new Code() {
                coding = new Coding[] {
                    new Coding() {
                        system = "http://",
                        code = "3637",
                        display = "Gene",
                    },
                },
                text = "Dip",
            },
            subject = new Subject() {
                reference = "Pat",
                display = "",
            },
            valueString = "*1/*1",
            component = new Component[] {
                new Component() {
                    code = new Code2(){
                        coding = new Coding[] {
                            new Coding(){
                                system = "http://",
                                code = "3637",
                                display = "Gene",
                            },
                        },
                    },
                    valueCodeableConcept = new Valuecodeableconcept(){
                        coding = new Coding[] {
                            new Coding(){
                                system = "http://",
                                code = "",
                                display = "CYP",
                            },
                        },
                    },
                },
            },
        };

        return obj;
    }
}

    public class Rootobject
    {
        public string resourceType { get; set; }
        public Code code { get; set; }
        public Subject subject { get; set; }
        public string valueString { get; set; }
        public Component[] component { get; set; }
    }

    public class Code
    {
        public Coding[] coding { get; set; }
        public string text { get; set; }
    }

    public class Coding
    {
        public string system { get; set; }
        public string code { get; set; }
        public string display { get; set; }
    }

    public class Subject
    {
        public string reference { get; set; }
        public string display { get; set; }
    }

    public class Component
    {
        public Code2 code { get; set; }
        public Valuecodeableconcept valueCodeableConcept { get; set; }
    }

    public class Code2
    {
        public Coding[] coding { get; set; }
    }

    public class Valuecodeableconcept
    {
        public Coding[] coding { get; set; }
    }

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