【问题标题】:how to return json format from ODATA?如何从 ODATA 返回 json 格式?
【发布时间】:2010-08-12 14:09:56
【问题描述】:

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

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

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

【问题讨论】:

  • 这是一个hack,但是如果你想默认返回JSON,在你的ODataConfig(或WebApiConfig)中的App_Start下你可以将这行代码添加到你的Register方法的底部:@987654323 @.
  • idk 但我必须将json 全部写成小写,然后才能正常工作。也许是CaSeSeNsItIvE?
  • 这里的结果相同,我只需要用小写写json。或者,将请求标头设置为 Accept: application/json 也可以
  • 对我来说,将其添加到我的网址中:?&$format=json

标签: json odata


【解决方案1】:

下载并安装 Fiddler。

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/

【讨论】:

  • 提琴手说 schemas.microsoft.com/ado/2007/08/dataservices/metadata"> 请求的媒体类型不受支持。
  • 我用的是 Postman,但我需要知道的是请求头 accept: application/json 谢谢!
【解决方案2】:

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

Accept: application/json;odata=verbose

在请求头中。

Accept: application/json

已经不够用了。更多信息here

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      查看this link

      【讨论】:

      • 说的有道理,这周我会试试,让你们知道谢谢
      • 恕我直言,这是正确的答案。但是请注意,某些 OData Web 服务仅支持 Atom (xml)。
      【解决方案5】:

      如果您使用 Data Services 中的 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>
      {
           ...
      

      【讨论】:

        【解决方案6】:

        "...但是我使用 http://test.com/feed2/ODataService.svc/results?$format=JSON ..."

        得到“找不到网页”

        Uri 中不需要 $format=JSON。

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

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

        【讨论】:

          【解决方案7】:

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

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

          【讨论】:

            【解决方案8】:

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

            “格式=json”

            【讨论】:

            • 那行不通。 @Cesar Miguel 提供的解决方案有效。在 URL 中添加 ?&$format=json
            【解决方案9】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-05-08
              • 2016-09-08
              • 1970-01-01
              • 1970-01-01
              • 2013-09-21
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多