C#对象表示JSON字符串

时间:2014-03-13 15:26:15

标签: c# json

我有以下JSON字符串:

{
    "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"
        }
    }
}

我想知道这个(见下文)是否是C#中JSON字符串的正确表示:

public class Region 
{
    public Region() { }
}

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

public class East : Region
{
    public 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字符串。

2 个答案:

答案 0 :(得分: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; }
}

答案 1 :(得分:0)

为什么要使用继承?

您的Json描述关系,而关系

层次结构:

  

区域 中心,东

     

按钮 保存

     

字段 标签

     

标签具有 firstName,lastName,chooseLocale

粗体是root