如何从 ODATA 返回 json 格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3468563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:33:27  来源:igfitidea点击:

how to return json format from ODATA?

jsonodata

提问by wil

I know ODATA can return json but not sure if I have to use an attribute or interface to do so.

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

I want it to do just like http://odata.netflix.com/Catalog/Titles?$format=JSON but my odata service doesn't return JSON. When I call it like www.foo.com/service?$format=json, it just returns XML.

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

What do I need to do to return json with ODATA?

我需要做什么才能用 ODATA 返回 json?

回答by lamarant

Download and install Fiddler.

下载并安装 Fiddler。

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

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

Once installed, open it, click on the "Request Builder" tab located in the right side of Fiddler.

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

Insert this URL:

插入这个网址:

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

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

Note that you DO NOT NEED THE ?$format=JSON

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

In the "Request Headers" section, insert the following line:

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

accept: application/json

Hit the Big "Execute" button at the top right of Fiddler.

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

You'll see the results of the request added to the list on the left side of Fiddler.

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

Double click on the request. The right side of Fiddler will change to the "Inspectors" tab where you can see the results of your request.

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

Also, since you are working with Json, you probably want to download and install the Json viewer plugin for Fiddler:

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

http://jsonviewer.codeplex.com/

http://jsonviewer.codeplex.com/

回答by Nate Cook

Newer versions of WCF Data Services support JSON by default and you must have

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

Accept: application/json;odata=verbose

in the request header.

在请求头中。

Accept: application/json

is no longer sufficient. More info here.

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

回答by Oliver Gray

No-one seems to be answering your question very cleanly here!

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

From an HTML page you can use the following Javascript / JQuery code to have a WCF Data Service return data in JSON format;

从 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>

回答by Glaxalg

You need to add “Accept: application/json” into the request header section.

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

Check out this link

看看这个链接

回答by zackdever

If you're using the ODATA provider from Data Services you can easily return ODATA as JSON by specifying it in the URL as in the sample you gave - http://odata.netflix.com/Catalog/Titles?$format=JSON

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

To do this use the JSONp and URL-controlled format support for ADO.NET Data Services download from MSDN http://code.msdn.microsoft.com/DataServicesJSONPand add the JSONPSupportBehavior decorator to your DataService class like below.

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

[JSONPSupportBehavior]
public class MyDataService : DataService<MyContextType>
{
     ...

回答by user4816271

"...but I get "The webpage cannot be found" using http://test.com/feed2/ODataService.svc/results?$format=JSON ..."

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

you dont need the $format=JSON in the Uri.

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

Just use "http://test.com/feed2/ODataService.svc/results"

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

(with Accept: application/json in the request header)

(在请求头中使用 Accept: application/json)

回答by user4816271

Late answer, but I've been spending the last hour trying to figure out how to curl OData APIs and return the result as json. The following code fetches the document in json and writes it to a file:

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

-o myfile.html -H "Accept: application/json" http://example.com/api/data?$filter=name eq 'whatever'

回答by Rados?aw Kiela

... just use lower case letters:

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

"format=json"

“格式=json”

回答by Steven Veltema

It's not pretty but this is how I forced JSON output without using $format in the request string:

这并不漂亮,但这就是我在请求字符串中不使用 $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();