0

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; }
    }
5
  • And what is the real problem? using of JsonConvert.Serialize( instance )? Commented May 13, 2017 at 10:03
  • Some of what it is listing has it not in order. I cannot change the order of the request or it fails. Some of the "root object " is below some of the others while some is on top. So even using nested objects did not seem to work or using object of objects as it just went in order. i have to be missing something easy. Commented May 13, 2017 at 10:15
  • Do you like answers like "Well put some of the others on top and the others to the bottom"? If not, then update your question with the code you serialize the instance and paste the output string you get from that Commented May 13, 2017 at 10:45
  • 1
    Check out the fiddle dotnetfiddle.net/wny4aP Commented May 13, 2017 at 11:06
  • Thank you Sir Rufo that was exactly what I was trying to do but could not figure it out. Too many commas for my brain i guess. Commented May 13, 2017 at 11:28

1 Answer 1

1

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; }
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.