c#将多个嵌套对象序列化为json

时间:2017-05-13 09:54:59

标签: c# json serialization json.net

我需要从Objects中序列化它,或者可能是c#中对象的对象。我已经尝试了多种不同的方法来获得相同的输出并且失败了。这是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"
           }
         ]
       }
     }
   ]
 }

我从中创建了以下对象。我试图制作一个包含它们的对象,然后使用Newtonsoft来序列化它,但我无法正确

这是对象

     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; }
    }

1 个答案:

答案 0 :(得分:1)

正如Rufo爵士指出的那样(为了完整答案)

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; }
    }