本文介绍了如何使用C#以标准格式响应json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我正在使用Web服务并以Web方式将结果作为json格式进行响应。在这里,我希望json格式如下所示
hello i am working with web services and response the result in web method as json format. here i wanted the json format as like below
{"Response":{"Status":"Success","Data":[{"StudentName":"Student Name","ClassName":"Class Name","DivisionName":"Div Name"}]}}
但我的输出显示如下
but my output is displayed like this
{"Response":[{"Status":"Success","Data":{"StudentName":"Student Name","ClassName":"Class Name","DivisionName":"Class Name"}}]}
请帮我以标准格式显示。
我尝试过:
这是我的代码
please help me to display in standard format.
What I have tried:
this is my code
struct ReturnValues
{
public string Status;
public object Data;
}
public class Login
{
public string StudentName { get; set; }
public string ClassName { get; set; }
public string DivisionName { get; set; }
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void Login(string UserName, string Password)
{
var ResultResponse = new Dictionary<string, ReturnValues[]>();
ResultResponse.Add("Status", new ReturnValues[1]);
if (username == "" && Password == "")
{
Login stud = new Login();
stud.StudentName = "Student Name";
stud.ClassName = "Class Name";
stud.DivisionName = "Div Name";
ResultResponse["Response"][0] = new ReturnValues { Status = "Success", Data = stud };
}
else
{
ResultResponse["Response"][0] = new ReturnValues { Status = "Fail", Data = "Invalid UserName Or Password" };
}
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(ResultResponse));
}
推荐答案
这篇关于如何使用C#以标准格式响应json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!