读书人

json参数设置有关问题

发布时间: 2013-10-29 12:07:57 作者: rapoo

json参数设置问题
本帖最后由 daijinhua 于 2013-10-26 11:25:56 编辑 需要输出的参数


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。
[解决办法]
json字符串的序列化和反序列化。 http://www.csharpwin.com/csharpspace/10822r2908.shtml

读书人网 >asp.net

热点推荐