Why does the following code return "[]" when it should return "{" id ": 1999," title ":" hithere "}
JavaScriptSerializer serializer = new JavaScriptSerializer(); StringBuilder sbJsonResults = new StringBuilder(); var result = serializer.Serialize(new dg(1999, "hithere")); context.Response.Clear(); context.Response.ContentType = "application/json; charset=utf-8"; context.Response.Cache.SetExpires(DateTime.MinValue); context.Response.Write(result);
PS class dg is as follows:
public class dg : ScheduleObserver, ILibrary, IEnumerable { public int id; public string title; private List<d> dList; ...many getters and setters and some logic functions... } public abstract class ScheduleObserver{ public abstract void update(); } public interface ILibrary { List<PD> getPDs(); void setPDs(List<PD> newPDs); int getCurrentIndex(); void addPD(PD pD); PD getPD(int index); }
Thank you very much.
THANKS - RESPONSIBLE SUCCESSFULLY - IEnumerable became the source of my problems. To solve this problem, dg no longer extends IEnumerable, and all foreach loops (dg ...) were converted back to for loops (int i = 0 ...).
THANKS VERY VERY MUCH! James understood why it was empty, Parv understood why it had square brackets. Mark as the answer, but also mark only one.
source share