1

I currently have a WebGet method that returns JSON correctly with the nested { d: } element, is there a way to remove this?

Here is my Server Side Code and my class defining the JSON return:

[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:

{
"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
    }
]
}
Matt Taylor
  • 3,360
  • 1
  • 20
  • 34
Jacques Bronkhorst
  • 1,685
  • 6
  • 34
  • 64
  • 1
    What do you mean, 'remove'? As in get the object? Yeah, just use a JSON parser (GSON or JSON.simple are two good ones) to extract the JSONArray using the tag 'd' – Matt Taylor Feb 15 '13 at 09:14
  • No I mean removing the D so it just shows: **[ jsondata ]** Without the d – Jacques Bronkhorst Feb 15 '13 at 09:39
  • Again, how do you want this object? As I said, you can extract the JSONArray using one of the above libraries, then you've got a JSONArray, which is the **[jsondata]**, as you put it. And if you then want the string, you just use the `.toString()` method, which will give you a JSONstring which is just the JSON data – Matt Taylor Feb 15 '13 at 10:23

1 Answers1

1

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

Matt Taylor
  • 3,360
  • 1
  • 20
  • 34