使用C#反序列化JSON以返回项目

时间:2016-12-02 12:51:36

标签: c# json

我有以下内容:

{"documents":
    [{"keyPhrases":
        [
            "search results","Azure Search","fast search indexing","sophisticated search            capabilities","Build        great   search experiences","time-sensitive search          scenarios","service availability","managed      service","service               updates","index corruption","near-instantaneous responses","multiple                    languages","integrated Microsoft    natural language stack","multiple           indexes","application       changes","ranking models","great relevance","years  of          development","primary interaction       pattern","storage","Bing","data             volume","rich","suggestions","hassle of     dealing","Reliable                  throughput","website","incremental cost","complexity","faceting","traffic","mobile                  apps","business goals","users","applications","user expectations","Office"
        ],
    "id":"1"}],
   "errors":[]
}

我需要在keyPhrases中提取项目,但绝对不知道如何去做。

我尝试了以下内容:

KeyPhraseResult keyPhraseResult = new KeyPhraseResult();

/// <summary>
/// Class to hold result of Key Phrases call
/// </summary>
public class KeyPhraseResult
{
    public List<string> keyPhrases { get; set; }
}

keyPhraseResult = JsonConvert.DeserializeObject<KeyPhraseResult>(content);

content包含上面的JSON字符串。

然而keyPhraseResult返回一个空值。

任何身体都可以帮助我朝正确的方向发展吗?

谢谢。

1 个答案:

答案 0 :(得分:4)

public class Document
{
    public List<string> keyPhrases { get; set; }
    public string id { get; set; }
}

public class RootObject
{
    public List<Document> documents { get; set; }
    public List<object> errors { get; set; }
}

你应该有这样的结构:

var result = JsonConvert.DeserializeObject<RootObject>(content);