简体   繁体   English

WCF服务返回列表。 需要在一个Json字符串中返回2个对象

[英]WCF Service returning list. Need to return 2 objects in one Json string

This is my first stack overflow post so cut me some slack :). 这是我的第一个堆栈溢出帖子,所以给我减少了一些懈怠:)。

I have been struggling quite a while now with this issue. 我在这个问题上已经挣扎了很长时间。

Currently my WCF reads data from a database and returns it as JSON. 当前,我的WCF从数据库读取数据,并将其作为JSON返回。

This is how it looks: 它是这样的:

{
    "shoppinglistitemsResult": [
        {
            "description": "this is my notes description",
            "name": "mynotename",
            "pid": "1",
            "status": "1",
            "username": "test"
        }
    ]
}

I want it to look like this: 我希望它看起来像这样:

{
    "shoppinglistitemsResult": [
        {
            "description": "123",
            "name": "123",
            "pid": "123",
            "status": "123",
            "username": "test"
        }
    ],
    "success": 1
}

With the extra object at the end. 最后有多余的对象。

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "displayAllNotes?name={username}&pass={password}")]
List<Service1.wsNotes> shoppinglistitems(string username, string password);

You would need to return an object that contains both the list and the success property instead of the list directly. 您将需要返回一个既包含列表又包含成功属性的对象,而不是直接包含列表的对象。 Think of every set of curly braces in JSON as a new object/class that needs to be created and everything that is comma separated as properties on that object. 可以将JSON中的花括号的每个集合视为需要创建的新对象/类,并将所有逗号分隔为该对象的属性。 So your outer curly braces would need to be represented by a class with two properties (shoppinglistitemsResult and success). 因此,您的外部花括号将需要由具有两个属性(shoppinglistitemsResult和success)的类表示。 You would need a second class for all of the items in your list. 您将需要清单中所有项目的第二类。

Here's a way you can do this with generics. 这是使用泛型执行此操作的方法。 I've also taken the liberty to include a couple of additional properties you might want to use. 我还自由地添加了一些您可能想使用的其他属性。 I've also included a Response type with no "Result" for operations that do not need to return values, but might want to return a success or error message. 对于不需要返回值但可能想要返回成功或错误消息的操作,我还包括了一个没有“结果”的响应类型。

[DataContract]
public class Response : IExtensibleDataObject
{
    public Response()
    {
        Success = true;
        ErrorMessage = null;
    }

    [DataMember]
    public bool Success { get; set; }
    [DataMember]
    public string ErrorMessage { get; set; }

    public ExtensionDataObject ExtensionData { get; set; }
}

[DataContract]
public class Response<TResult> : Response
{
    [DataMember]
    public TResult Result { get; set; }
}

And then your operation contract would look something like this... 然后您的运营合同将如下所示:

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "displayAllNotes?name={username}&pass={password}")]
Response<List<Notes>> GetShoppingListItems();

然后构建一个类,该类包含一个列表和所需的其他对象作为成员,并以json形式返回该类的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM