简体   繁体   English

不包含{“ d:”元素的JSON返回方法

[英]Method to return JSON without { “d: ” element

I currently have a WebGet method that returns JSON correctly with the nested { d: } element, is there a way to remove this? 我目前有一个WebGet方法,该方法可以使用嵌套的{d:}元素正确返回JSON,是否可以删除此方法?

Here is my Server Side Code and my class defining the JSON return: 这是我的服务器端代码和我的定义JSON返回的类:

[OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json)]
    public List<OpenOrderHeader> GetOpenOrders()
    {
        return (from OH in SCMENT.CustomerOrders
                select new OpenOrderHeader()
                {
                    CustomerID = OH.CustomerId,
                    OrderID = OH.CustomerOrderID,
                    OrderUniqueNumber = OH.CustomerOrderUniqueNumber,
                    OrderDateCreated = OH.CustomerOrderDateCreated,
                    OrderDateUpdated = OH.CustomerOrderDateUpdated,
                    OrderLocalOnline = OH.CustomerOrderOnlineOrLocal,
                    BranchID = (int)OH.CustomerOrderLocalBranchID,
                    OrderCurrency = OH.CustomerOrderCurrency,
                    OrderConversionRate = OH.CustomerOrderCurrencyConversionRate,
                    OrderStatus = OH.CustomerOrderStatus
                }
                ).ToList();
    }

The Class: 班级:

  public class OpenOrderHeader
{
    public int CustomerID { get; set; }
    public int OrderID { get; set; }
    public int OrderUniqueNumber { get; set; }
    public int NumberOfProductsOnOrder { get; set; }
    public DateTime OrderDateCreated { get; set; }
    public DateTime OrderDateUpdated { get; set; }
    public string OrderLocalOnline { get; set; }
    public int BranchID { get; set; }
    public string PaymentMethod { get; set; }
    public int OrderStatus { get; set; }
    public string OrderCurrency { get; set; }
    public decimal OrderConversionRate { get; set; }
    public decimal SubTotalInclTax { get; set; }
    public decimal SubTotalExclTax { get; set; }
    public decimal DiscountInclTax { get; set; }
    public decimal DiscountExclTax { get; set; }
    public decimal ShippingInclTax { get; set; }
    public decimal ShippingExclTax { get; set; }
    public decimal PaymentFeeInclTax { get; set; }
    public decimal PaymentFeeExclTax { get; set; }


}

the JSON: JSON:

{
"d": [
    {
        "__type": "OpenOrderHeader:#POS.Tracntrace.Member_Only.DAL.Models.Order",
        "BranchID": -1,
        "CustomerID": -1,
        "DiscountExclTax": 0,
        "DiscountInclTax": 0,
        "NumberOfProductsOnOrder": 0,
        "OrderConversionRate": 1,
        "OrderCurrency": "ZAR",
        "OrderDateCreated": "/Date(1359696634387+0200)/",
        "OrderDateUpdated": "/Date(1359703834253+0200)/",
        "OrderID": 1,
        "OrderLocalOnline": "ONLINE",
        "OrderStatus": 10,
        "OrderUniqueNumber": 10,
        "PaymentFeeExclTax": 0,
        "PaymentFeeInclTax": 0,
        "PaymentMethod": null,
        "ShippingExclTax": 0,
        "ShippingInclTax": 0,
        "SubTotalExclTax": 0,
        "SubTotalInclTax": 0
    },
    {
        "__type": "OpenOrderHeader:#POS.Tracntrace.Member_Only.DAL.Models.Order",
        "BranchID": -1,
        "CustomerID": -1,
        "DiscountExclTax": 0,
        "DiscountInclTax": 0,
        "NumberOfProductsOnOrder": 0,
        "OrderConversionRate": 1,
        "OrderCurrency": "ZAR",
        "OrderDateCreated": "/Date(1359712050660+0200)/",
        "OrderDateUpdated": "/Date(1359719250317+0200)/",
        "OrderID": 2,
        "OrderLocalOnline": "ONLINE",
        "OrderStatus": 10,
        "OrderUniqueNumber": 11,
        "PaymentFeeExclTax": 0,
        "PaymentFeeInclTax": 0,
        "PaymentMethod": null,
        "ShippingExclTax": 0,
        "ShippingInclTax": 0,
        "SubTotalExclTax": 0,
        "SubTotalInclTax": 0
    },
    {
        "__type": "OpenOrderHeader:#POS.Tracntrace.Member_Only.DAL.Models.Order",
        "BranchID": -1,
        "CustomerID": -1,
        "DiscountExclTax": 0,
        "DiscountInclTax": 0,
        "NumberOfProductsOnOrder": 0,
        "OrderConversionRate": 1,
        "OrderCurrency": "ZAR",
        "OrderDateCreated": "/Date(1359713291023+0200)/",
        "OrderDateUpdated": "/Date(1359720490673+0200)/",
        "OrderID": 3,
        "OrderLocalOnline": "ONLINE",
        "OrderStatus": 10,
        "OrderUniqueNumber": 12,
        "PaymentFeeExclTax": 0,
        "PaymentFeeInclTax": 0,
        "PaymentMethod": null,
        "ShippingExclTax": 0,
        "ShippingInclTax": 0,
        "SubTotalExclTax": 0,
        "SubTotalInclTax": 0
    },
    {
        "__type": "OpenOrderHeader:#POS.Tracntrace.Member_Only.DAL.Models.Order",
        "BranchID": -1,
        "CustomerID": -1,
        "DiscountExclTax": 0,
        "DiscountInclTax": 0,
        "NumberOfProductsOnOrder": 0,
        "OrderConversionRate": 1,
        "OrderCurrency": "ZAR",
        "OrderDateCreated": "/Date(1359947946093+0200)/",
        "OrderDateUpdated": "/Date(1359955145467+0200)/",
        "OrderID": 4,
        "OrderLocalOnline": "ONLINE",
        "OrderStatus": 10,
        "OrderUniqueNumber": 13,
        "PaymentFeeExclTax": 0,
        "PaymentFeeInclTax": 0,
        "PaymentMethod": null,
        "ShippingExclTax": 0,
        "ShippingInclTax": 0,
        "SubTotalExclTax": 0,
        "SubTotalInclTax": 0
    },
    {
        "__type": "OpenOrderHeader:#POS.Tracntrace.Member_Only.DAL.Models.Order",
        "BranchID": -1,
        "CustomerID": -1,
        "DiscountExclTax": 0,
        "DiscountInclTax": 0,
        "NumberOfProductsOnOrder": 0,
        "OrderConversionRate": 1,
        "OrderCurrency": "ZAR",
        "OrderDateCreated": "/Date(1359948641833+0200)/",
        "OrderDateUpdated": "/Date(1359955841153+0200)/",
        "OrderID": 5,
        "OrderLocalOnline": "ONLINE",
        "OrderStatus": 10,
        "OrderUniqueNumber": 14,
        "PaymentFeeExclTax": 0,
        "PaymentFeeInclTax": 0,
        "PaymentMethod": null,
        "ShippingExclTax": 0,
        "ShippingInclTax": 0,
        "SubTotalExclTax": 0,
        "SubTotalInclTax": 0
    },
    {
        "__type": "OpenOrderHeader:#POS.Tracntrace.Member_Only.DAL.Models.Order",
        "BranchID": -1,
        "CustomerID": -1,
        "DiscountExclTax": 0,
        "DiscountInclTax": 0,
        "NumberOfProductsOnOrder": 0,
        "OrderConversionRate": 1,
        "OrderCurrency": "ZAR",
        "OrderDateCreated": "/Date(1359948752250+0200)/",
        "OrderDateUpdated": "/Date(1359955951577+0200)/",
        "OrderID": 6,
        "OrderLocalOnline": "ONLINE",
        "OrderStatus": 10,
        "OrderUniqueNumber": 15,
        "PaymentFeeExclTax": 0,
        "PaymentFeeInclTax": 0,
        "PaymentMethod": null,
        "ShippingExclTax": 0,
        "ShippingInclTax": 0,
        "SubTotalExclTax": 0,
        "SubTotalInclTax": 0
    },
    {
        "__type": "OpenOrderHeader:#POS.Tracntrace.Member_Only.DAL.Models.Order",
        "BranchID": -1,
        "CustomerID": -1,
        "DiscountExclTax": 0,
        "DiscountInclTax": 0,
        "NumberOfProductsOnOrder": 0,
        "OrderConversionRate": 1,
        "OrderCurrency": "ZAR",
        "OrderDateCreated": "/Date(1359948895973+0200)/",
        "OrderDateUpdated": "/Date(1359956095290+0200)/",
        "OrderID": 7,
        "OrderLocalOnline": "ONLINE",
        "OrderStatus": 10,
        "OrderUniqueNumber": 16,
        "PaymentFeeExclTax": 0,
        "PaymentFeeInclTax": 0,
        "PaymentMethod": null,
        "ShippingExclTax": 0,
        "ShippingInclTax": 0,
        "SubTotalExclTax": 0,
        "SubTotalInclTax": 0
    },
    {
        "__type": "OpenOrderHeader:#POS.Tracntrace.Member_Only.DAL.Models.Order",
        "BranchID": -1,
        "CustomerID": -1,
        "DiscountExclTax": 0,
        "DiscountInclTax": 0,
        "NumberOfProductsOnOrder": 0,
        "OrderConversionRate": 1,
        "OrderCurrency": "ZAR",
        "OrderDateCreated": "/Date(1359949020693+0200)/",
        "OrderDateUpdated": "/Date(1359956220013+0200)/",
        "OrderID": 8,
        "OrderLocalOnline": "ONLINE",
        "OrderStatus": 10,
        "OrderUniqueNumber": 17,
        "PaymentFeeExclTax": 0,
        "PaymentFeeInclTax": 0,
        "PaymentMethod": null,
        "ShippingExclTax": 0,
        "ShippingInclTax": 0,
        "SubTotalExclTax": 0,
        "SubTotalInclTax": 0
    },
    {
        "__type": "OpenOrderHeader:#POS.Tracntrace.Member_Only.DAL.Models.Order",
        "BranchID": -1,
        "CustomerID": -1,
        "DiscountExclTax": 0,
        "DiscountInclTax": 0,
        "NumberOfProductsOnOrder": 0,
        "OrderConversionRate": 1,
        "OrderCurrency": "USD",
        "OrderDateCreated": "/Date(1359957893433+0200)/",
        "OrderDateUpdated": "/Date(1359965238550+0200)/",
        "OrderID": 9,
        "OrderLocalOnline": "ONLINE",
        "OrderStatus": 40,
        "OrderUniqueNumber": 1,
        "PaymentFeeExclTax": 0,
        "PaymentFeeInclTax": 0,
        "PaymentMethod": null,
        "ShippingExclTax": 0,
        "ShippingInclTax": 0,
        "SubTotalExclTax": 0,
        "SubTotalInclTax": 0
    }
]
}

From what you've been saying, I think something like this should solve your problem: 从您所说的来看,我认为这样可以解决您的问题:

JSONObject returnedObject = Your JSON data as returned from the server

JSONArray desiredArray = returnedObject.getJSONArray("d");

//retrieve the data and results from the JSONArray
for (int jsonCounter = 0; jsonCounter < desiredArray.length(); jsonCounter++) { 
    //Get the next object
    JSONObject nextobj = desiredArray.getJSONObject(jsonCounter);

    //Get the data
    item1= nextobj.getString("__type");
    item2= nextobj.getString("BranchID");
    etc......
}

And that's your basic structure to extract the data. 这就是提取数据的基本结构。 From the code you've posted I'm not exactly sure where this would go, but it should be enough to help you on your way and extract your data. 从您发布的代码中,我不确定要去哪儿,但是它足以帮助您前进并提取数据。 Also bear in mind that the above code was written in Android using the included JSON parser, so will require some alterations to make it work with a different library 还请记住,以上代码是使用随附的JSON解析器在Android中编写的,因此需要进行一些更改才能使其与其他库一起使用

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

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