[英]Parsing WordPress REST API post tags
I'm attempting to get the categories and tags related to a WordPress post. 我正在尝试获取与WordPress帖子相关的类别和标签。
It's a self-hosted installation with the Jetpack plugin installed and working, so I'm using the WordPress.com Rest API to query. 这是一个自托管的安装,安装了Jetpack插件并且可以正常工作,因此我正在使用WordPress.com Rest API进行查询。
Here's some example JSON which the REST API returns for /posts
这是 REST API为
/posts
返回的一些示例JSON
You'll note that the first post returned in the above example (at the time of writing) is tagged facebook
, Google
, Publicize
, Sharing
and twitter
. 您会注意到,在上述示例中(在撰写本文时)返回的第一篇文章被标记为
facebook
, Google
, Publicize
, Sharing
和twitter
。
However there's something of an inconsistency with how the JSON is formatted. 但是,JSON的格式存在一些不一致之处。 Instead of being represented as an array with every object in the array having consistent properties (name, slug, description) - allowing me to simply iterate the array - each tag seems to be represented as its own object, with the object's name being the name of the tag.
与其被表示为数组,数组中的每个对象都具有一致的属性(名称,子句,描述)-允许我简单地对数组进行迭代-每个标签似乎都表示为自己的对象,而对象的名称为名称标签。 Here's how it shows in the JSON Viewer :
这是它在JSON Viewer中的显示方式:
So, without knowing the tags in advance, it presents a challenge of how to get the post's tags. 因此,在不事先知道标签的情况下,就提出了如何获取帖子标签的挑战。
I'm using .NET 4.5's HttpClient
so I've worked around it, of a fashion, using the dynamic
type: 我使用的是.NET 4.5的
HttpClient
所以我使用dynamic
类型来解决了这一问题:
public class WordpressBlogPost : IBlogPost
{
// IBlogPost implementation
public DateTime Date { get; set; }
public DateTime Modified { get; set; }
public string Title { get; set; }
public string URL { get; set; }
public string Content { get; set; }
public string Excerpt { get; set; }
public dynamic Tags { get; set; }
public string Featured_Image { get; set; }
public int Comment_Count { get; set; }
}
You can then iterate Tags, were each is a Newtonsoft.Json.Linq.JProperty
and access the name, slug, etc. like so: 然后,您可以迭代Tag,每个都是
Newtonsoft.Json.Linq.JProperty
并访问名称,子弹等,如下所示:
foreach (var wordpressPost in parseJsonResult.posts)
{
foreach (var tag in wordpressPost.Tags)
{
string tagName = tag.Value.name;
string tagSlug = tag.Value.slug;
}
}
However, it feels quite hacky, and I'd like a cleaner way of doing it. 但是,它感觉很hacky,我想要一种更干净的方法。
So - does anyone know - 所以-有人知道-
dynamic
type so I can have strongly typed tags? dynamic
类型更好的方法,所以我可以使用强类型标记? Objects in JSON are in fact associative arrays (aka: hash, dictionaries), and you can loop on them, just like any other array. JSON中的对象实际上是关联数组(也称为哈希,字典),您可以像其他数组一样在它们上循环。
In C#, you can do so like this: 在C#中,您可以这样做:
foreach(KeyValuePair<string, string> entry in MyDic)
See also https://stackoverflow.com/questions/141088/what-is-the-best-way-to-iterate-over-a-dictionary-in-c 另请参阅https://stackoverflow.com/questions/141088/what-is-the-best-way-to-iterate-over-a-dictionary-in-c
Hoping this will help. 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.