2017-01-23 8 views
0

からJSONをデシリアライズすることができます私は、次のJSONは、私が見つけることを試みている何のDocumentumはどのように私はDocumentumのRESTサービス

{ 
    "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"] 
      } 
     } 
    } 
} 

から返されていたクラスにこれを変換する方法です。

私は

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

を試してみました、私は次のクラスを定義し、その中にそれをデシリアライズしようとしています。

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

    public void Dispose() 
    { 

    } 
} 
+1

http://json2csharp.comのようなオンラインPOCO発電機を試してみませんか? – Miki

答えて

1

残念ながら、DeserializeObjectは、単純にそれをしてはいけません。あなたは簡単な解決策が必要な場合は、JSONにLINQを使用しようとする場合があります。

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

あなたも、そうのような更なる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(); 

コードの場合は、DeserializeObjectと一緒に使用するカスタムJsonConverterにする必要があります。

関連する問題