将JSON数组值转换为字符串

时间:2015-04-27 15:55:45

标签: c# arrays json

我有一个JSON数组,我从中获取了所有值,但我没有看到将特定值作为字符串数组返回的方法。

public class News
{
    public string author { get; set; }
    public string title { get; set; }
    public string content { get; set; }
    public string contentSnippet { get; set; }
    public string link { get; set; }
    public string publishedDate { get; set; }

    public string[] getFeed(string Website)
    {
        string path = @"http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=" + Website;

        string json = new WebClient().DownloadString(path);
        JObject jsonObject = JObject.Parse(json);
        JArray array = (JArray) jsonObject["responseData"]["feed"]["entries"];
        var content = array.Select(token => JsonConvert.DeserializeObject<News>(token.ToString())).ToList();

        return new string[] { content.ToString() }; // I thought this could work but it's not.
    }
}

我正在使用Newtonsoft的JSON lib对其进行反序列化。

如果要检查JSON文件:http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=https%3A%2F%2Fnews.google.com%2Fnews%3Fpz%3D1%26cf%3Dall%26hl%3Dpt-BR%26output%3Drss

我用来更好地可视化结构的工具:http://jsonschema.net/#/

1 个答案:

答案 0 :(得分:0)

@Shaharyar解决了问题。

public class News
{
    public string author { get; set; }
    public string title { get; set; }
    public string content { get; set; }
    public string contentSnippet { get; set; }
    public string link { get; set; }
    public string publishedDate { get; set; }

    public List<News> getFeed(string Website)
    {
        string path = @"http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=" + Website;

        string json = new WebClient().DownloadString(path);
        JObject jsonObject = JObject.Parse(json);
        JArray array = (JArray) jsonObject["responseData"]["feed"]["entries"];

        var content = array.Select(token => JsonConvert.DeserializeObject<News>(token.ToString())).ToList();

        return content;
    }
}

谢谢!

相关问题