开发者

how to return json format from ODATA?

开发者 https://www.devze.com 2023-01-11 12:29 出处:网络
I know ODATA can return json but not sure if I have to use an attribute or interface to do so. I want it to do just like http://odata.netflix.com/Catalog/Titles?$format=JSON but 开发者_运维问答my oda

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

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.

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


Download and install Fiddler.

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

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

Insert this URL:

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

Note that you DO NOT NEED THE ?$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.

You'll see the results of the request added to the list on the left side of 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.

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

http://jsonviewer.codeplex.com/


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

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;

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

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

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.

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


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

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

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

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


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:

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


... just use lower case letters:

"format=json"


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号