如何正确组织muli-objects JSON的C#应用​​程序设计?

时间:2012-12-01 21:05:18

标签: c# .net json

我的C#应用​​程序的架构问题有问题。我需要有以下JSON:

{data:{lat:22,lng:33}, error:{code:0,description:""}}

我正在尝试使用这些类实现此JSON结构:

public class Response
{
    public object Data { get; set; }
    public object Error { get; set; }

    public Response()
    {
        Error = new ErrorsManager();
    }
}

public class Coordinates : Response
{
    public double Lat { get; set; }
    public double Lng { get; set; }
}

我可以毫无问题地为 错误 设置值。但是如何设置 数据 的值我无法理解。有人能为这种情况提出适当的解决方案吗?我知道这个问题可以通过Java中的 extends 来解决。我可以在C#中使用类似的东西吗?

更新:我想在JSON中拥有常见的根对象 - 数据和错误。但是数据对象可以具有不同的上下文,例如:坐标,用户数据等。

3 个答案:

答案 0 :(得分:2)

您可以将Data设置为坐标实例。这是这样的:

public class Response
{
    public object Data { get; set; }
    public object Error { get; set; }

    public Response()
    {
        Error = new ErrorsManager();
        Data = new Coordinates();
    }
}

public class Coordinates {
    public double Lat { get; set; }
    public double Lng { get; set; }
}

您可能会考虑创建强类型的Response版本,意思是这样的:

public class Response
{
    public Coordinates Data { get; set; }
    public ErrorsManager Error { get; set; }

    public Response()
    {
        Error = new ErrorsManager();
        Data = new Coordinates();
    }
}

如果您有不同的响应类型,您可能希望有一个仅定义Error的基类和添加新属性的子类。我假设您正在使用类似JSON.NET的内容来序列化/反序列化。

你可能是通用的:

public class Response<T> where T : new() {
    public T Data { get; set; }
    public ErrorsManager Error { get; set; }

    public Response() {
        Error = new ErrorsManager();
        Data = new T();
    }
}

public class Coordinates {
    /* left away */
}

用法:

var response = new Response<Coordinates>();
response.Data.Lat = 0.01;
// whatever you like

答案 1 :(得分:1)

C#:

String JSON = (new JavaScriptSerializer()).Serialize(
    new { data = new { lat = 22; lng = 33; }; error = new {code = 0; description=String.Empty;}; }
);

或者:

using System.Web.Script.Serialization;

    public class myResponse
    {
        public Coordinates data;
        public Error error;

        public myResponse()
        {
            data = new Coordinates();
            error = new Error();
        }
    }

    public class Coordinates
    {
        public double lat;
        public double lng;
    }

    public class Error
    {
        public int code;
        public String description;
    }

    var resp = new myResponse();
    resp.data.lat = 0.3453453453;
    resp.data.lng = 0.3453453453;
    resp.error.code = 0;
    resp.error.description = "Description of error";

    String JSON = (new JavaScriptSerializer()).Serialize(resp);

答案 2 :(得分:1)

创建类型安全类,而不是将它们声明为对象

public class Response
{
    public Coordinates Data { get; set; }
    public Error Error { get; set; }
}

public class Error
{
    public int code { get; set; }
    public string description { get; set; }
}

public class Coordinates 
{
    public double Lat { get; set; }
    public double Lng { get; set; }
}

您可以将json字符串反序列化为

string jsonStr = @"{data:{lat:22,lng:33}, error:{code:0,description:""aaa""}}";

var jObj = JsonConvert.DeserializeObject<Response>(jsonStr); //Json.Net

var jObj2 = new JavaScriptSerializer().Deserialize<Response>(jsonStr); 

您还可以使用Json.Net完全动态方式,而无需声明任何这些类

dynamic dynObj = JsonConvert.DeserializeObject(jsonStr); 
Console.WriteLine(dynObj.data.lat);
Console.WriteLine(dynObj.error.description);