我正在编写 C# 代码来访问 Swift 对象存储。第一步是根据 Keystone 身份服务对用户进行身份验证。我已成功完成此操作并获得以下 JSON 字符串作为响应。
{
"access":{
"token":{
"expires": "2013-10-22T17:09:46Z",
"id": "c6f69256db4d45af819cc42b54e18f69",
"tenant": {
"enabled": true,
"id": "7b9a902423a582c9eda266dcf3ad697420c1c3ff9429b1dfd255152f3bf2098f",
"name": "tenant1"}
},
"serviceCatalog":[
{
"endpoints":
[
{
"adminURL": "http://mysite.com:8888/",
"region": "RegionOne",
"internalURL":"http://mysite.com:8888/v1/AUTH_7b9a902423a52d255152f3bf2098f",
"id": "4ec67a5dcd034a68ad08a2fc133a8dc0",
"publicURL": http://mysite.com:8888/v1/AUTH_7b9a902423a52d255152f3bf2098f
}
],
"endpoints_links": [],
"type": "object-store",
"name": "swift"}
],
"user":{
"username": "user1",
“roles_links": [],
"id": "324c2ae86fff69d22629320cdf589f417b9a902423a582c9eda266dcf3ad6974",
"roles":[
{"name": "tenant1"},
{"name": "Likewise Users"},
{"name": "tenant1"}
],
"name": "user1"
},
"metadata":{
"is_admin": 0,
"roles":[
"7b9a902423a582c9eda266dcf3ad697420c1c3ff9429b1dfd255152f3bf2098f",
"7b9a902423a582c9eda266dcf3ad6974104ff417849b613ce82c28d1562f82c8",
"7b9a902423a582c9eda266dcf3ad697420c1c3ff9429b1dfd255152f3bf2098f"
]
}
}
}
我有使用 JSON.net 4.0.3 的代码,该代码使用一种相当粗略的方法(我的代码,而不是 newtonsoft)将信息从 JSON 中提取出来并放入我可以用来引用我需要的属性的字典集合中。我的代码看起来像这样。
var accessRoot = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonResponse);
var accessItems = JsonConvert.DeserializeObject<Dictionary<string, object>>(accessRoot["access"].ToString());
var tokens = JsonConvert.DeserializeObject<Dictionary<string, object>>(accessItems["token"].ToString());
var tokenTenantDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(tokens["tenant"].ToString());
var serviceCatalog = JsonConvert.DeserializeObject<Dictionary<string, object>>(accessItems["serviceCatalog"].ToString());
var user = JsonConvert.DeserializeObject<Dictionary<string, object>>(accessItems["user"].ToString());
var metadata = JsonConvert.DeserializeObject<Dictionary<string, object>>(accessItems["metadata"].ToString());
string tokenExpiration = tokens["expires"].ToString();
string tokenId = tokens["id"].ToString();
string tokenTenant = tokens["tenant"].ToString();
string userUsername = user["username"].ToString();
string tenantId = tokenTenantDict["id"];
string userId = user["id"].ToString();
string userName = user["name"].ToString();
我正在寻找的是一个更优雅的解决方案,它可能包括一个可以将 JSON 字符串反序列化为的类定义。我还没有找到类似的东西,我希望答案将对其他人对 Keystone 界面进行编程有很大帮助。感谢您提供任何建议的解决方案。