使用C#反序列化JSON以返回项目 [英] Deserializing JSON using C# to return items

查看:146
本文介绍了使用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内的项目,但绝对不知道该怎么做.

I need to extract the items within the keyPhrases, but have absolutely no idea how to do it.

我尝试了以下方法:

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字符串.

content contains the JSON string above.

但是keyPhraseResult返回一个空值.

However the keyPhraseResult returns a null value.

有人能在正确的方向上帮助我吗?

Could any body help me in the right direction ?

谢谢.

推荐答案

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

这篇关于使用C#反序列化JSON以返回项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆