我有一个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对其进行反序列化。
我用来更好地可视化结构的工具:http://jsonschema.net/#/
答案 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;
}
}
谢谢!