35

我知道 ODATA 可以返回 json,但不确定我是否必须使用属性或接口来这样做。

我希望它像http://odata.netflix.com/Catalog/Titles ?$format=JSON 但我的 odata 服务不返回 JSON。当我像 www.foo.com/service?$format=json 一样调用它时,它只返回 XML。

我需要做什么才能返回带有 ODATA 的 json?

4

9 回答 9

26

下载并安装提琴手。

http://www.fiddler2.com/fiddler2/

安装后,打开它,单击位于 Fiddler 右侧的“Request Builder”选项卡。

插入此网址:

http://test.com/feed2/ODataService.svc/results

请注意,您不需要 ?$format=JSON

在“请求标头”部分中,插入以下行:

accept: application/json

点击 Fiddler 右上角的大“执行”按钮。

您将看到请求的结果已添加到 Fiddler 左侧的列表中。

双击请求。Fiddler 的右侧将变为“Inspectors”选项卡,您可以在其中查看请求的结果。

此外,由于您使用的是 Json,您可能需要下载并安装 Fiddler 的 Json 查看器插件:

http://jsonviewer.codeplex.com/

于 2011-05-10T21:06:51.400 回答
13

较新版本的 WCF 数据服务默认支持 JSON,您必须具有

Accept: application/json;odata=verbose

在请求头中。

Accept: application/json

已经不够了。更多信息在这里

于 2013-05-10T19:40:22.307 回答
9

似乎没有人在这里非常干净地回答您的问题!

在 HTML 页面中,您可以使用以下 Javascript/JQuery 代码让 WCF 数据服务以 JSON 格式返回数据;

    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">

        var sURL = "http://YourService.svc/Books(10)";

        function testJSONfetch() {

            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: sURL,
                error: bad,
                success: good,
                beforeSend: function (XMLHttpRequest) {
                    //Specifying this header ensures that the results will be returned as JSON.
                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                }
            });

        }

        function good(response)
        {

        }

        function bad(response) 
        {

        }

    </script>
于 2012-01-06T14:12:40.510 回答
7

您需要在请求标头部分添加“Accept: application/json”。

看看这个链接

于 2010-08-12T22:04:32.057 回答
4

如果您使用数据服务中的 ODATA 提供程序,您可以通过在您提供的示例中的 URL 中指定它来轻松地将 ODATA 作为 JSON 返回 - http://odata.netflix.com/Catalog/Titles ?$format=JSON

为此,请使用从 MSDN http://code.msdn.microsoft.com/DataServicesJSONP下载的 ADO.NET 数据服务的 JSONp 和 URL 控制格式支持,并将 JSONPSupportBehavior 装饰器添加到您的 DataService 类,如下所示。

[JSONPSupportBehavior]
public class MyDataService : DataService<MyContextType>
{
     ...
于 2012-01-10T20:17:12.930 回答
2

“...但是我使用http://test.com/feed2/ODataService.svc/results ?$format=JSON ...”得到“找不到网页”

你不需要 Uri 中的 $format=JSON 。

只需使用“ http://test.com/feed2/ODataService.svc/results

(在请求标头中带有 Accept: application/json)

于 2010-08-28T01:46:39.907 回答
1

迟到的答案,但我花了最后一个小时试图弄清楚如何卷曲 OData API 并将结果作为 json 返回。以下代码以 json 格式获取文档并将其写入文件:

-o myfile.html -H "Accept: application/json" http://example.com/api/data?$filter=name eq 'whatever'
于 2013-05-02T22:18:15.950 回答
1

...只需使用小写字母:

“格式=json”

于 2016-02-14T21:14:31.040 回答
0

这并不漂亮,但这就是我在请求字符串中不使用 $format 的情况下强制 JSON 输出的方式:

    Request r = new Request(Method.GET, "http://XXXXXXX.svc//Login"
                 + "&UserId=" + "'" + "user" + "'" 
                 + "&Password=" + "'" + "password" + "'");

    ClientInfo ci = r.getClientInfo();
    ArrayList<Preference<MediaType>> accepted = new ArrayList<Preference<MediaType>>();
    accepted.add(new Preference<MediaType>(MediaType.APPLICATION_JSON));
    ci.setAcceptedMediaTypes(accepted);

    Client client = new Client(Protocol.HTTP);  
    Response response = client.handle(r);  
    Representation output = response.getEntity();  
于 2011-11-09T02:25:28.277 回答