首页新闻找找看学习计划

JSON 序列化类

0
悬赏园豆:5 [已关闭问题] 关闭于 2016-06-17 09:36

{

"err_code": 0,  //错误码,int类型

"err_msg":"", //错误描述,String类型

     "data_array": {

        "data_list": [

            {"key1":"","key2":"" },{"key1":"" , "key2":"" }

        ],

        "page_now": 1,  //当前页

        "page_size": 10,  //每页多少条

        "page_count": 20 //总页数

    }

}

//生成以上JSON?

public class AjaxjsonResultlists<T>
{
public int err_code { get; set; }
public string err_msg { get; set; }
public string data_array { get; set; }
}

public class AjasjsonResultlist<T>
{
public List<T> data_list { get; set; }
public int page_now { get; set; } //当前页
public int page_size { get; set; } //每页多少条
public int page_count { get; set; } //总页数
}

 

public string test()

{

AjaxjsonResultlists<Class> r = new AjaxjsonResultlists<Class>();

AjasjsonResultlist<Class> rs = new AjasjsonResultlist<Class>();

JavaScriptSerializer js = new JavaScriptSerializer();

string _json_str = string.Empty;

List<Class> list = new List<Class>();

rs.data_list = list;
rs.page_count = list.Count;
rs.page_now = 0;
rs.page_size = 10;

r.err_code = 0;
r.err_msg = "查询信息成功!";
r.data_array = _json_str;
_json_str = js.Serialize(r);

 

return _json_str ;

}

 

这样写生成的有问题,请问大神,我该怎么改呀!绕去绕来,都晕了。

有机物的主页 有机物 | 初学一级 | 园豆:18
提问于:2016-06-17 09:24
< >
分享
所有回答(2)
0

public class AjaxjsonResultlists<T>
{
public int err_code { get; set; }
public string err_msg { get; set; }
public AjasjsonResultlist<T> data_array { get; set; }
}

public class AjasjsonResultlist<T>
{
public List<T> data_list { get; set; }
public int page_now { get; set; } //当前页
public int page_size { get; set; } //每页多少条
public int page_count { get; set; } //总页数
}

 

rs.data_list = list;
rs.page_count = list.Count;
rs.page_now = 0;
rs.page_size = 10;

r.err_code = 0;
r.err_msg = "查询信息成功!";
r.data_array = rs;
_json_str = js.Serialize(r);

有机物 | 园豆:18 (初学一级) | 2016-06-17 09:36
0

public class ResultObj
{
  public int err_code{ get; set; }
  public string err_msg{ get; set; }
  public object data_array{ get; set; }
}

public ActionResult Test(){

  Object obj= ...; //放结果的数组对象

  return Json(new ResultObj(){err_code=1,err_msg="你错了",data_array=new { data_list=obj,page_now=1, page_size =9,page_count =5}}, JsonRequestBehavior.AllowGet);

}

 

ICE_Inspire | 园豆:228 (菜鸟二级) | 2016-06-21 15:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册