简体   繁体   中英

Deserialize keystone json response using newtonsoft json.net

I am writing C# code to access a Swift object store. The first step is to authenticate the user against the Keystone identity service. I've done this successfully and get the following JSON string in response.

{
  "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. 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. I haven't found anything like this and I expect the answer will be a big help to others programming to the Keystone interface. Thank you for any suggested solutions.

The reason this does not work right is that there is an error in the JSON string. I use the on-line checker to validate a JSON string that is received before using it in my code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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