1

我目前有一个 WebGet 方法,它使用嵌套的 { d: } 元素正确返回 JSON,有没有办法删除它?

这是我的服务器端代码和定义 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();
    }

班上:

  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; }


}

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
    }
]
}
4

1 回答 1

1

从您所说的来看,我认为这样的事情应该可以解决您的问题:

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......
}

这就是您提取数据的基本结构。从您发布的代码中,我不确定这会去哪里,但它应该足以帮助您完成您的工作并提取您的数据。还要记住,上面的代码是使用包含的 JSON 解析器在 Android 中编写的,因此需要进行一些更改才能使其与不同的库一起使用

于 2013-02-15T10:32:09.357 回答