需要输出的参数
page_id: "526a30e689c09768798a2338", status :"200", question:{ _id={$oid="526b168189c0971b820a6fea"},custom_attr={},has_open_option=false,jumpconstraint_id_list=[],matrixrow_id_list=[], option_id_list=['526b168189c0971b820a6feb','526b168189c0971b820a6fec'], option_list=[{_id={$oid="526b13d889c0971b4853104e"},cid="A1",custom_attr={text_row="1", text_col="20"},is_open=false,question_id="526b13d789c0971b4853104d",title="选项1",updated={$date=1382777944003} }], project_id="526a30e689c09768798a2337",question_type=6,questionpage_id=52537aff89c09748bc880a42,title="填空题", updated={$date=1382777943968}}
ashx页面代码:
private HttpRequest Request { get; set; } private HttpResponse Response { get; set; } JavaScriptSerializer jss = new JavaScriptSerializer(); public void ProcessRequest(HttpContext context) { Request = context.Request; Response = context.Response; Response.ContentType = "text/html"; Response.Expires = -1; try { //测试 string ss = Request.Form["questionpage_id"]; object resp = "{ _id={$oid=\"526b168189c0971b820a6fea\"},custom_attr={},has_open_option=false,jumpconstraint_id_list=[],matrixrow_id_list=[], option_id_list=['526b168189c0971b820a6feb','526b168189c0971b820a6fec'], option_list=[{_id={$oid=\"526b13d889c0971b4853104e\"},cid=\"A1\",custom_attr={text_row=\"1\", text_col=\"20\"},is_open=false,question_id=\"526b13d789c0971b4853104d\",title=\"选项1\",updated={$date=1382777944003} }], project_id=\"526a30e689c09768798a2337\",question_type=6,questionpage_id=52537aff89c09748bc880a42,title=\"填空题\", updated={$date=1382777943968}}"; var strB = new UploadResponse() { status = 200, page_id = ss, question = resp }; Response.Write(jss.Serialize(strB)); } catch { } Response.End(); } public class UploadResponse { public int status { get; set; } public string page_id { get; set; } public object question { get; set; } } public bool IsReusable { get { return false; } } } }
现在有个问题就是需要接口代码那边“question”的类型必须是object,所以我上面那种直接拼凑的方法就不行了,求解需要怎么写比较好? 再写一个这种吗
public class Question { public object _id { get; set; } public object custom_attr { get; set; } public bool has_open_option { get; set; } }
,那上面的 _id={$oid=\"526b168189c0971b820a6fea\"}格式怎么设定呢,类似于jumpconstraint_id_list=[]这种的又怎么设置呢?
首先,对于object,你还可以obj as Model嘛。
对于Json对应的实体
public class Question { public object _id { get; set; } public object custom_attr { get; set; } public bool has_open_option { get; set; } public List<Model> Models{get;set;}//这样的写法就为转换为json的数组对象。 }
是需要多写一个 question 类
然后使用 Newtonsoft.Json
很容易得到你要的输出