1

Currently I try to convert List object to Json string, this drive me crazy on my ASP.net MVC project.

Model:

public class PagerBase<T>:List<T> where T:EntityBase
{
    public int totalpage {get;set;}
    public int pageindex {get;set;}
    public int pagesize  {get;set;}

    public PagerBase(IEnumerable<T> source, int totalpage , int pageindex ,int pagesize)
    {
        this.totalpage = totalpage ;
        this.pageindex = pageindex;
        this.pagesize = pagesize;
        this.AddRange(source);
    }
}

My Action like this

public JsonResult GetClient()
{
    int pagesize=20;
    int pageindex1;

    var providers = getclient(20,1);
    var totalpage= gettotalpage(20);

    var pagerclient=new PagerBase<Client>(providers,totalpage,pageindex,pagesize);
    JavaScriptSerializer serializer = new JavaScriptSerializer();

    var jsonstring=serializer.Serialize(pagerclient);
    JsonResult jr = Json(new
    {
        Html = jsonstring,
        Message = "Ok"
    }, JsonRequestBehavior.AllowGet);

    return jr;
}

I want result at least include totalpage, pagesize, pageindex but actually I got empty string if there is no client.

2
  • I see so many of these questions and still just can't understand why anyone is using this shitty JsonResult library (other than maybe because it's in system.web.mvc but that's not a good reason). If you're open to using json.NET instead I will provide an answer but that JsonResult class is just a joke and I won't bother trying to correct any code that uses it :p Commented Jan 24, 2014 at 19:08
  • Thank you answer my question, my question is not about JsonResult. Commented Jan 24, 2014 at 19:17

1 Answer 1

3

JSON can't encode data that both has properties and is an array. I'm sure you could write a custom serializer if you use Json.NET, but if possible, it'd be easier to just refactor your class a bit:

public class PagerBase<T> where T:EntityBase
{
    public List<T> items {get;set;}
    public int totalpage {get;set;}
    public int pageindex {get;set;}
    public int pagesize  {get;set;}

    public PagerBase(IEnumerable<T> source, int totalpage, int pageindex, int pagesize)
    {
        this.items = source.ToList();
        this.totalpage = totalpage;
        this.pageindex = pageindex;
        this.pagesize = pagesize;
    }
}

As an aside, I'd recommend you switch to Json.NET. It's much better than JavaScriptSerializer.

Sign up to request clarification or add additional context in comments.

5 Comments

BTW, I heard lot people said Json.Net is much better, I want to know how better about Json.Net
Speed, flexibility, ease of use, etc. Near the bottom of the Json.NET web site, it has a list of features of it vs other libraries. I'd say the biggest difference for you is the ease of customizing things.
I just review json.Net web site, it does lot feature compare MS's function, I will go this right now. Thanks.
@Ericyu67 I would say Tim's assessment is pretty accurate. For me the big thing is it's no more difficult for the common use cases but is way more full featured than JsonResult so when you want to do more advanced things it's an option. If you build a big project using JsonResult everywhere then find you need to do something more advanced you're going to be between a rock and a hard place.
You are right, I do lot thing base on JsonResult because I may don't know there is another option can do move advance. Now I realize there is something can be improve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.