简体   繁体   English

如何从ODATA返回json格式?

[英]how to return json format from ODATA?

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. 我希望它像http://odata.netflix.com/Catalog/Titles?$ format = JSON一样,但我的odata服务不返回JSON。 When I call it like www.foo.com/service?$format=json, it just returns XML. 当我把它称为www.foo.com/service?$format=json时,它只返回XML。

What do I need to do to return json with ODATA? 使用ODATA返回json需要做什么?

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右上角的Big“Execute”按钮。

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的右侧将更改为“Inspectors”选项卡,您可以在其中查看请求的结果。

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/

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 . 更多信息在这里

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>

You need to add “Accept: application/json” into the request header section. 您需要在请求标头部分添加“Accept:application / json”。

Check out this link 看看这个链接

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提供程序,您可以轻松地将ODATA作为JSON返回,方法是在URL中指定它,如您提供的示例所示 - 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/DataServicesJSONP and add the JSONPSupportBehavior decorator to your DataService class like below. 为此,请使用从MSDN http://code.msdn.microsoft.com/DataServicesJSONP下载的ADO.NET数据服务的JSONp和URL控制格式支持,并将JSONPSupportBehavior装饰器添加到您的DataService类,如下所示。

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

"...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)

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. 迟到的答案,但我一直在花费最后一小时试图弄清楚如何卷曲OData API并将结果作为json返回。 The following code fetches the document in json and writes it to a file: 以下代码在json中获取文档并将其写入文件:

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

... just use lower case letters: ...只使用小写字母:

"format=json" “格式= JSON”

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();  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM