以 ASP.Net 中的结构格式解析 Json 数据(Visual Studio 2013)
Parse Json Data in Structure format in ASP.Net (Visual Studio 2013)
我只希望 JSON 数据是 return 的特定格式,例如在所有细节之上的一个数组。请浏览包含我想要实现的代码和输出的图片。
public IEnumerable <Data> Get()
{
return new List<Data> {
new Data {Id=1,Content="Sample Content",Author="Bhanu"},
new Data {Id=2,Content="Sample Content",Author="War"}
};
}
上面的代码是用 Controllers.cs 编写的,数据是从模型 class 中获取的,它具有如下代码中的 ID、内容、作者声明。
namespace WebApplication1.model
{
public class Data
{
public int Id { get; set; }
public string Content { get; set;}
public string Author { get; set; }
}
}
您需要使用 属性 类型的 IEnumerable 创建模型 class,如下所示:
public class DataModel
{
public IEnumerable<Data> Something {get; set; }
}
然后return上面提到的一个实例class:
public DataModel Get()
{
return new DataModel { Something = new List<Data> {
new Data {Id=1,Content="Sample Content",Author="Bhanu"},
new Data {Id=2,Content="Sample Content",Author="War"}
}
};
}
我只希望 JSON 数据是 return 的特定格式,例如在所有细节之上的一个数组。请浏览包含我想要实现的代码和输出的图片。
public IEnumerable <Data> Get()
{
return new List<Data> {
new Data {Id=1,Content="Sample Content",Author="Bhanu"},
new Data {Id=2,Content="Sample Content",Author="War"}
};
}
上面的代码是用 Controllers.cs 编写的,数据是从模型 class 中获取的,它具有如下代码中的 ID、内容、作者声明。
namespace WebApplication1.model
{
public class Data
{
public int Id { get; set; }
public string Content { get; set;}
public string Author { get; set; }
}
}
您需要使用 属性 类型的 IEnumerable 创建模型 class,如下所示:
public class DataModel
{
public IEnumerable<Data> Something {get; set; }
}
然后return上面提到的一个实例class:
public DataModel Get()
{
return new DataModel { Something = new List<Data> {
new Data {Id=1,Content="Sample Content",Author="Bhanu"},
new Data {Id=2,Content="Sample Content",Author="War"}
}
};
}