获取 JSON 格式的 OData $metadata
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18683338/
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
Get OData $metadata in JSON format
提问by Omar
is it possible to get metadata of an OData service in JSON format?
是否可以以 JSON 格式获取 OData 服务的元数据?
When I try to use format=json, it doesn't work. Here is what I tried:
当我尝试使用时format=json,它不起作用。这是我尝试过的:
http://odata.informea.org/services/odata.svc/$metadata/?format=json
回答by Jen S
The $metadatadocument is in the CSDL format, which currently only has an XML representation. (As a side note, if you do want to request the json format for a different kind of OData payload, make sure the formatquery token has a $in front of it: $format=json.)
该$metadata文档采用 CSDL 格式,目前只有 XML 表示。(附带说明,如果您确实想要为不同类型的 OData 负载请求 json 格式,请确保format查询令牌$前面有一个:$format=json。)
So, no it is not possible. You can, however, get the service document in JSON, which is a subset of the $metadata document:
所以,不,这是不可能的。但是,您可以获取 JSON 格式的服务文档,它是 $metadata 文档的一个子集:
http://odata.informea.org/services/odata.svc?$format=json
This won't have type information, but it will list the available entry points of the service (i.e., the entity sets).
这不会有类型信息,但会列出服务的可用入口点(即实体集)。
回答by Thierry Templier
I agreed with the previous answer. This isn't supported by the specification but some OData frameworks / libraries are about to implement this feature.
我同意之前的回答。规范不支持此功能,但一些 OData 框架/库即将实现此功能。
I think about Olingo. This is could be helpful for you if you also implement the server side. See this issue in the Olingo JIRA for more details:
我想到了奥林戈。如果您还实现了服务器端,这可能对您有所帮助。有关更多详细信息,请参阅 Olingo JIRA 中的此问题:
- OLINGO-570- https://issues.apache.org/jira/browse/OLINGO-570
- OLINGO-570- https://issues.apache.org/jira/browse/OLINGO-570
Hope it helps you, Thierry
希望对你有帮助,蒂埃里
回答by John Slegers
As an alternative to ?$format=json, you could also just set the following two headers :
作为 的替代方案?$format=json,您还可以设置以下两个标头:
Accept: application/jsonContent-Type: application/json; charset=utf-8
Accept: application/jsonContent-Type: application/json; charset=utf-8
I'm not sure which is the minimum Odata version required, but this works perfectly for me on Microsoft Dynamics NAV 2016, which uses Odata v4.
我不确定哪个是所需的最低 Odata 版本,但这在 Microsoft Dynamics NAV 2016 上非常适合我,它使用 Odata v4。
回答by Michael Sebastian
You can use jQuery to get the relevant information from an OData service $metadata.
您可以使用 jQuery 从 OData 服务 $metadata 获取相关信息。
Take for example:
You write a unit test to check the OData entities property names matches with your application entities. Then you have to retrieve the properties of the OData entity.
举个例子:
您编写一个单元测试来检查 OData 实体属性名称与您的应用程序实体是否匹配。然后您必须检索 OData 实体的属性。
$.ajax({
type: "GET",
url: "/destinations/odata-service/$metadata",
beforeSend: function() {
console.log("before send check");
},
dataType: "xml",
contentType: "application/atom+xml",
context: document.body,
success: function(xml) {
console.log("Success ResourceTypes");
var ODataTypeINeed = $(xml).find('EntityType').filter(function(){
return $(this).attr('Name') == 'ODataTypeINeed'
});
$(ODataTypeINeed).find('Property').each(function() {
console.log($(this).attr('Name')); //List of OData Entity properties
});
},
error: function(err) {
console.log(err);
}
});
回答by johnny 5
I wrote a simple provider to parse out some of the needed information from the metadata, Feel free to expand on it. First you'll need some simple models, to expresss the data, we'll want to convert from there ugly XML names
我写了一个简单的提供程序来从元数据中解析出一些需要的信息,请随意扩展它。首先你需要一些简单的模型来表达数据,我们需要从那里转换丑陋的 XML 名称
export class ODataEntityType
{
name: string;
properties: ODataProperty[];
}
export class ODataProperty
{
name: string;
type: ODataTypes;
isNullable: boolean;
}
//Hack Until Ionic supports TS 2.4
export class ODataTypeMap
{
"Edm.Int32" = ODataTypes.Int;
"Edm.Int64" = ODataTypes.Long;
"Edm.Decimal" = ODataTypes.Decimal;
"Edm.Double" = ODataTypes.Double;
"Edm.Guid" = ODataTypes.Guid;
"Edm.String" = ODataTypes.String;
"Edm.Boolean" = ODataTypes.Bool;
"Edm.DateTime" = ODataTypes.DateTime;
"Edm.DateTimeOffset" = ODataTypes.DateTimeOffset;
}
export enum ODataTypes
{
Int,
Long,
Decimal,
Double,
Guid,
String,
Bool,
DateTime,
DateTimeOffset
}
This is the provider:
这是提供者:
import { Injectable } from "@angular/core";
import { Http } from "@angular/http";
import * as X2JS from 'x2js';
import * as _ from 'underscore';
import { ODataEntityType, ODataProperty, ODataTypes, ODataTypeMap } from "../models/ODataEntityType";
@Injectable()
export class ODataMetadataToJsonProvider {
x2js = new X2JS();
public entityTypeMap: Dictionary = new Dictionary();
public entityTypes : ODataEntityType[];
constructor(public http: Http) {
}
parseODataMetadata(metadataUrl: string) {
this.http.get(metadataUrl).subscribe(data => {
let metadata: any = this.x2js.xml2js(data.text());
let rawEntityTypes = _.filter(metadata.Edmx.DataServices.Schema, x => x["EntityType"] != null);
if(rawEntityTypes.length == 0)
{
return;
}
this.entityTypes = _.map(rawEntityTypes[0]["EntityType"], t => {
let oDataEntityType = new ODataEntityType();
oDataEntityType.name = t["_Name"];
oDataEntityType.properties = _.map(t["Property"], p => {
let property = new ODataProperty();
property.name = p["_Name"];
let typeStr: string = p["_Type"];
property.type = ODataTypeMap[typeStr];
property.isNullable = !!p["_Nullable"];
return property;
});
return oDataEntityType;
});
});
}
}

