简体   繁体   English

使用newtonsoft json.net反序列化Keystone JSON响应

[英]Deserialize keystone json response using newtonsoft json.net

I am writing C# code to access a Swift object store. 我正在编写C#代码来访问Swift对象存储。 The first step is to authenticate the user against the Keystone identity service. 第一步是根据Keystone身份服务对用户进行身份验证。 I've done this successfully and get the following JSON string in response. 我已成功完成此操作,并得到以下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"
      ]
    }
  }
}

I have code using JSON.net 4.0.3 that takes uses a rather crude approach (my code, not newtonsoft) to pulling the information out of the JSON and into a collection of dictionaries that I can use to reference the attributes I need. 我有使用JSON.net 4.0.3的代码,该代码采用了一种相当粗糙的方法(我的代码,不是newtonsoft)将信息从JSON中提取出来,并放入字典集合中,可以用来引用所需的属性。 My code looks like this. 我的代码如下所示。

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

What I'm looking for is a more elegant solution that probably includes a class definition that I can deserialize the JSON string into. 我正在寻找的是一种更优雅的解决方案,其中可能包括一个可以反序列化JSON字符串的类定义。 I haven't found anything like this and I expect the answer will be a big help to others programming to the Keystone interface. 我还没有找到这样的东西,我希望答案将对其他对Keystone界面进行编程的人有很大帮助。 Thank you for any suggested solutions. 感谢您提出任何建议的解决方案。

The reason this does not work right is that there is an error in the JSON string. 这样做不正确的原因是JSON字符串中有错误。 I use the on-line checker to validate a JSON string that is received before using it in my code. 我使用在线检查器来验证在我的代码中使用它之前接收到的JSON字符串。

http://www.freeformatter.com/json-validator.html http://www.freeformatter.com/json-validator.html

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

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