首页 新闻 赞助 找找看

json参数设置问题

0
悬赏园豆:100 [已解决问题] 解决于 2013-10-29 21:19

需要输出的参数

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}}
View Code

 

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;
}
}
}
}
View Code

 

 

现在有个问题就是需要接口代码那边“question”的类型必须是object,所以我上面那种直接拼凑的方法就不行了,求解需要怎么写比较好? 再写一个这种吗

public class Question
{
public object _id { get; set; }
public object custom_attr { get; set; }
public bool has_open_option { get; set; }

}
View Code

 

 ,那上面的 _id={$oid=\"526b168189c0971b820a6fea\"}格式怎么设定呢,类似于jumpconstraint_id_list=[]这种的又怎么设置呢?

小赖皮的主页 小赖皮 | 初学一级 | 园豆:71
提问于:2013-10-26 11:30
< >
分享
最佳答案
0

首先,对于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的数组对象。

}
收获园豆:60
幻天芒 | 高人七级 |园豆:37175 | 2013-10-26 11:40
其他回答(2)
0

是需要多写一个 question 类

然后使用 Newtonsoft.Json

很容易得到你要的输出

收获园豆:20
Yu | 园豆:12980 (专家六级) | 2013-10-26 13:12
0

Newtonsoft.Json 直接用这个序列化成json

在这下载你对应项目的版本:

http://www.nuget.org/packages/newtonsoft.json/

收获园豆:20
悟行 | 园豆:12559 (专家六级) | 2013-10-26 16:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册