2014-03-13 87 views
0

我有以下JSON字符串:C#對象表示

{ 
    "region" : { 
     "center" : { 
      "title" : "Center Region" 
      }, 
      "east" : { 
       "title" : "East Region - Form" 
      } 
     }, 
     "buttons" : { 
      "save" : "Save" 
     }, 
     "fields" : { 
      "labels" : { 
       "firstName" : "First Name", 
       "lastName" : "Last Name", 
       "chooseLocale" : "Choose Your Locale" 
      } 
     } 
} 

我想知道是否這(見下文)是JSON字符串的C#中的正確的表示:

public class Region 
{ 
    public Region() {} 
} 

public class Center : Region 
{ 
    public Center(){} 
    public string title{get; set;} 
} 

public class East : Region 
{ 
    pubic East(){} 
    public string title{get; set;} 
} 

public class Buttons 
{ 
    public Buttons(){} 
    public string save{get; set;} 
} 

public class Fields 
{ 
    public Fields(){} 
} 

public class Labels : Fields 
{ 
    public Labels(){} 
    public string firstName{get; set;} 
    public string lastName{get; set;} 
    public string chooseLocale{get; set;} 
} 

我需要正確的對象表示,然後我可以使用JsonConvert.SerializeObject(object);進行序列化,以生成上面的JSON字符串。

+5

http://json2csharp.com/工作的對待 – RoughPlace

+1

在ASP.NET和Web Tools 2012.2+中,您還有['將JSON粘貼爲類'] (http://blogs.msdn.com/b/webdev/archive/2012/12/18/paste-json-as-classes-in-asp-net-and-web-tools-2012-2-rc.aspx ) – khellang

回答

0

你爲什麼要使用繼承?

您的JSON描述關係,而不是關係

層次結構變爲:

區域中心,東

按鈕保存

領域標籤

標籤名字,姓氏,chooseLocale

大膽是根

1

試試這個

public class Center 
{ 
    public string title { get; set; } 
} 

public class East 
{ 
    public string title { get; set; } 
} 

public class Region 
{ 
    public Center center { get; set; } 
    public East east { get; set; } 
} 

public class Buttons 
{ 
    public string save { get; set; } 
} 

public class Labels 
{ 
    public string firstName { get; set; } 
    public string lastName { get; set; } 
    public string chooseLocale { get; set; } 
} 

public class Fields 
{ 
    public Labels labels { get; set; } 
} 

public class RootObject 
{ 
    public Region region { get; set; } 
    public Buttons buttons { get; set; } 
    public Fields fields { get; set; } 
} 
+4

我看你在那裏做什麼 – RoughPlace

+0

複製/粘貼很酷,哈哈? –

+1

嘗試jakin2csharp.com Yakyb指定:)。 – Yousuf