C#webservice添加额外节点

时间:2018-02-12 05:33:13

标签: c# json visual-studio web-services wcf

我正在编写一个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;
}

这是我用于从数据库中获取问题的Exam类。现在我很难编写Exam类。

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'以及如何摆脱它。感谢。

1 个答案:

答案 0 :(得分:3)

  

为什么c#在开始时添加'GetExamResult'以及如何摆脱它

这是由于BodyStyle = WebMessageBodyStyle.Wrapped。当BodyStyle被包装时,序列化程序将响应包装在额外的JSON(或XML)级别中(请参阅here)。

我知道这可能很麻烦,并不总是需要,但有些情况下需要Wrapped身体。当您返回裸值(作为intstring)时就是这种情况,因为您无法将简单值映射到JSON。实际上你的代码就是这种情况。使用ResponseFormat = WebMessageFormat.Json,您不打算运行时要返回JSON string,而是告诉运行时将返回值转换为有效的JSON。由于string无效JSON,因此需要将其包装在JSON中。

无论如何,有一种非常简单的方法可以让你实现你想要的东西(它甚至可以使你的代码更容易)。只需让您的Web方法直接返回ExamClass实例即可。你要做的只是注释ExamClass类(旁注:请不要用[{1}}后缀你的类名 - 这是不好的风格){{1和Class

的成员
DataContractAttribute

然后您可以直接从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; } } // ... } 的对象
ExamClass

这更多的是如何使用WPF。