如何格式化json显示并获得响应

时间:2014-09-11 02:46:43

标签: c# json wcf

public FooMessageType Foo(string name, string password)
{
    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["sample"].ToString());
    con.Open();
    SqlCommand cmd = new SqlCommand("Select * from Employee where username='" + name + "' and password='" + password + "'", con);
    SqlDataReader rd = cmd.ExecuteReader();
    if (rd.Read())
    {
        return new FooMessageType { Name = name, Password = password, msg = "welcome" };
    }
    else
    {
        return new FooMessageType { Name = name, Password = password, msg = "error" };
    }
}

[OperationContract]
[WebInvoke(Method = "POST",
   ResponseFormat = WebMessageFormat.Json,
   RequestFormat = WebMessageFormat.Json,
   BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "aw")]
FooMessageType Foo(string name, string password);

[DataContract]
public class FooMessageType
{
    [DataMember(Name = "name")]
    public string Name { get; set; }
    [DataMember(Name = "password")]
    public string Password { get; set; }
    [DataMember(Name = "msg")]
    public string msg { get; set; }
}

这是请求数据

{ "name": "123","password": "123"}

这是响应数据

{"msg":"error","name":"123","password":"123"}

我想要的是

{LoginRequest:{"username": "123","password":"123"}}

和回复

{LoginResponse:{"responseCode":"1","responseMessage":""
userDetails: {"firstname":"","lastname": ""}}}

任何人都可以帮我创建类来显示这些数据并获得响应吗?我是创建wcf的新手。

1 个答案:

答案 0 :(得分:0)

class OuterClass { 
    public InnerClass MyProperty { get; set; }
}

class InnerClass { ... }