2017-05-13 76 views
0

我需要序列化来自对象的对象,或者可能是c#中对象的对象。我尝试了多种不同的方式来获得相同的输出并失败。这是JSONc#将多个嵌套对象序列化为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; } 
    } 
+0

什么是真正的问题?使用JsonConvert.Serialize(实例)? –

+0

它列出的部分内容没有按顺序排列。我无法更改请求的顺序或失败。一些“根对象”在其他一些下面,而另一些则在顶端。所以即使使用嵌套对象似乎也没有工作或使用对象的对象,因为它只是按顺序进行。我必须错过简单的事情。 –

+0

你喜欢这样的答案:“把其他人放在最上面,其他人放在最下面”?如果没有,然后更新您的问题与您的序列化实例的代码,并粘贴从你得到的输出字符串 –

回答

1

由于入佛门先生指出的(对于答案的完整性)

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