从Odata服务中读取Json输出

时间:2015-04-14 16:55:39

标签: c# json wcf c#-4.0 odata

我正在尝试以json格式读取odata服务的输出。但我收到错误信息

"响应有效负载不是有效的响应有效负载。请确保顶级元素是有效的Atom或JSON元素或属于' http://schemas.microsoft.com/ado/2007/08/dataservices'命名空间"。

NorthwindEntities dc = new NorthwindEntities(new Uri("http://services.odata.org/V3/Northwind/Northwind.svc/"),DataServiceProtocolVersion.V3);
        dc.Format.UseJson(new EdmModel());
        DataServiceQuery<Product> query = (DataServiceQuery<Product>)from o in dc.Products
            where o.UnitPrice > 0
            select o; query.execute();


{"odata.metadata":"http://services.odata.org/V3/Northwind/Northwind.svc/$metadata#Products","value":[{"ProductID":24,"ProductName":"Guaran\u00e1 Fant\u00e1stica","SupplierID":10,"CategoryID":1,"QuantityPerUnit":"12 - 355 ml cans","UnitPrice":"4.5000","UnitsInStock":20,"UnitsOnOrder":0,"ReorderLevel":0,"Discontinued":true},{"ProductID":33,"ProductName":"Geitost","SupplierID":15,"CategoryID":4,"QuantityPerUnit":"500 g","UnitPrice":"2.5000","UnitsInStock":112,"UnitsOnOrder":0,"ReorderLevel":20,"Discontinued":false}]}

1 个答案:

答案 0 :(得分:0)

您的JSON字符串无效,因为它的属性名称为&#39;。&#39;在里面。 odata.metadata无法解析。

如果您将其更改为odata_metadata,或者可以使用此处提供的解决方案:enter link description here