C# webservice添加额外的节点
Posted
技术标签:
【中文标题】C# webservice添加额外的节点【英文标题】:C# webservice adding extra node 【发布时间】:2018-07-22 05:52:55 【问题描述】:我正在编写一个 C# 服务来生成问题和答案。我的服务代码如下
public interface IExamService
[OperationContract]
[WebInvoke(Method = "GET",UriTemplate = "/ExamQs",RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped)]
string GetExam();
public string GetExam()
List<ExamClass> exam = new List<ExamClass>();
for(int i=0; i < 4; i++)
ExamClass ex = new ExamClass();
ex.Question = "This is the Question no "+ i.ToString();
ex.Answer1 = "This is Answer1 of Question " + i.ToString();
ex.Answer2 = "This is Answer2 of Question " + i.ToString();
ex.Answer3 = "This is Answer3 of Question " + i.ToString();
ex.Answer4 = "This is Answer4 of Question " + i.ToString();
ex.Correct = 1;
exam.Add(ex);
string json = JsonConvert.SerializeObject(exam);
return json;
这是我用来从数据库中获取问题的考试课程。现在我正在对考试类进行硬编码。
namespace ExamService
public class ExamClass
string question;
string answer1;
string answer2;
string answer3;
string answer4;
Int32 correct;
public string Question
get
return question;
set
question = value;
public string Answer1
get
return answer1;
set
answer1 = value;
public string Answer2
get
return answer2;
set
answer2 = value;
public string Answer3
get
return answer3;
set
answer3 = value;
public string Answer4
get
return answer4;
set
answer4 = value;
public int Correct
get
return correct;
set
correct = value;
输出是
"GetExamResult":"[\"Question\":\"This is the Question no 0\",\"Answer1\":\"This is Answer1 of Question 0\",\"Answer2\":\"This is Answer2 of Question 0\",\"Answer3\":\"This is Answer3 of Question 0\",\"Answer4\":\"This is Answer4 of Question 0\",\"Correct\":1,\"Question\":\"This is the Question no 1\",\"Answer1\":\"This is Answer1 of Question 1\",\"Answer2\":\"This is Answer2 of Question 1\",\"Answer3\":\"This is Answer3 of Question 1\",\"Answer4\":\"This is Answer4 of Question 1\",\"Correct\":1,\"Question\":\"This is the Question no 2\",\"Answer1\":\"This is Answer1 of Question 2\",\"Answer2\":\"This is Answer2 of Question 2\",\"Answer3\":\"This is Answer3 of Question 2\",\"Answer4\":\"This is Answer4 of Question 2\",\"Correct\":1,\"Question\":\"This is the Question no 3\",\"Answer1\":\"This is Answer1 of Question 3\",\"Answer2\":\"This is Answer2 of Question 3\",\"Answer3\":\"This is Answer3 of Question 3\",\"Answer4\":\"This is Answer4 of Question 3\",\"Correct\":1,\"Question\":\"This is the Question no 4\",\"Answer1\":\"This is Answer1 of Question 4\",\"Answer2\":\"This is Answer2 of Question 4\",\"Answer3\":\"This is Answer3 of Question 4\",\"Answer4\":\"This is Answer4 of Question 4\",\"Correct\":1]"
我的问题是为什么 c# 在开始时添加“GetExamResult”以及如何摆脱它。谢谢。
【问题讨论】:
请发帖ExamClass
【参考方案1】:
为什么 c# 在开头添加 'GetExamResult' 以及如何摆脱它
这是由于BodyStyle = WebMessageBodyStyle.Wrapped
。当 BodyStyle
被包装时,序列化程序将响应包装在额外的 JSON(或 XML)级别(请参阅 here)。
我知道这可能很麻烦并且并不总是需要,但在某些情况下,需要或需要 Wrapped
正文。当您返回裸值(作为int
或string
)时就是这种情况,因为您无法将简单值映射到 JSON。实际上,您的代码就是这种情况。使用ResponseFormat = WebMessageFormat.Json
,您不会告诉运行时您打算返回 JSON string
,而是告诉运行时将您的返回值转换为有效的 JSON。 Ans 由于 string
不是有效的 JSON,因此需要将其包装在 JSON 中。
无论如何,有一种非常简单的方法可以让您实现您想要的(它甚至使您的代码更容易一些)。只需让您的 Web 方法直接返回一个 ExamClass
实例。您所要做的就是用DataContractAttribute
及其成员注释ExamClass
类(旁注:请不要在您的类名称后缀Class
- 这是不好的风格) DataMemberAttribute
[DataContract]
public class ExamClass
string question;
string answer1;
string answer2;
string answer3;
string answer4;
Int32 correct;
[DataMember]
public string Question
get
return question;
set
question = value;
[DataMember]
public string Answer1
get
return answer1;
set
answer1 = value;
// ...
然后你可以直接从IExamService
返回一个ExamClass
的对象
public interface IExamService
[OperationContract]
[WebInvoke(Method = "GET",
UriTemplate = "/ExamQs",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
ExamClass GetExam();
这更多地说明了 WPF 的使用方式。
【讨论】:
以上是关于C# webservice添加额外的节点的主要内容,如果未能解决你的问题,请参考以下文章