简体   繁体   English

如何从Documentum REST Service反序列化JSON

[英]How can I deserialize JSON from Documentum REST Service

I have the following JSON returned from Documentum 我从Documentum返回了以下JSON

{
    "resources" : {
        "http://identifiers.emc.com/linkrel/repositories" : {
            "href" : "http://stg1docapp10:7000/dctm-rest/repositories.json",
            "hints" : {
                "allow" : ["GET"],
                "representations" : ["application/xml", "application/json", "application/atom+xml", "application/vnd.emc.documentum+json"]
            }
        },
        "about" : {
            "href" : "http://stg1docapp10:7000/dctm-rest/product-info.json",
            "hints" : {
                "allow" : ["GET"],
                "representations" : ["application/xml", "application/json", "application/vnd.emc.documentum+xml", "application/vnd.emc.documentum+json"]
            }
        }
    }
}

What I am trying to find is how to convert this into an class; 我试图找到的是如何将其转换为类。

I have tried 我努力了

Dictionary<String,String> ds = JsonConvert.DeserializeObject<Dictionary<String, String>>(result);

and I have tried defining the following class and deserializing it into that. 并且我尝试定义以下类并将其反序列化为该类。

public class DocumentumServices
    : IDisposable
{
    public String href { get; set; }
    public IList<String> hints { get; set; }

    public void Dispose()
    {

    }
}

Unfortunately, DeserializeObject cannnot do what you want that simply. 不幸的是, DeserializeObject无法简单地完成您想要的操作。 If you want a simple solution, you may try to use LINQ to JSON: 如果您想要一个简单的解决方案,则可以尝试使用LINQ to JSON:

var ds = JObject.Parse(result);
var list = (from doc in ds["resources"]
           select doc.Values().ToDictionary(k => ((JProperty)k).Name, 
                                            v => v.Children().First()) into gr 
           select new DocumentumServices() {
               href = gr["href"].ToString(),
               hints = (from hnt in gr["hints"] select hint.ToString()).ToList()
           }).ToList();

You can even decompose hints further like so: 您甚至可以进一步分解hints如下所示:

public class DocumentumServices
{
    public string href { get; set; }
    public Dictionary<string, List<string>> hints { get; set; }
}
// ...

var ds = JObject.Parse(result);
var list = (from doc in ds["resources"]
            select doc.Values().ToDictionary(k => ((JProperty)k).Name, v => v.Children().First()) into gr 
            select new DocumentumServices() {
                href = gr["href"].ToString(),
                hints = (from hint in gr["hints"]
                         select new {
                            Key = hint.Path.Substring(hint.Path.LastIndexOf('.')+1),
                            Value = hint.Children().First().Select(x => x.ToString()).ToList()
                        }).ToDictionary(k => k.Key, v => v.Value)
            }).ToList();

If you're intended to use this often in your code, you should make it into a custom JsonConverter to use with DeserializeObject . 如果打算在代码中经常使用它,则应将其放入自定义的JsonConverter以与DeserializeObject一起使用。

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

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