我在umbraco上通过听众在 IContent
对象上有一个句柄
public static IContent[] FaqEntities(this PublishEventArgs<IContent> publishEventArgs)
{
return publishEventArgs.PublishedEntities.Where(x => x.ContentType.Name == "FAQ").ToArray();
}
在c#中,我想创建一个来自IR返回数组中已发布的文档所选属性(bir和value(的JSON
文件 - 是否有一种简单的方法来序列化umbraco IContent
对象以获取我需要的JSON输出?
var json = JsonConvert.SerializeObject(faqEntities)
和 var json = Json.Encode(faqEntities)
只是给了我整个对象,但我希望创建一个类似于
{
"faqs": [{
"nodeId": 1,
"question": "My Password is incorrect?",
"answer": "If you have forgotten or lost your password, please click on the Forgotten Password link on the Login page and follow the instructions shown."
},
{
"nodeId": 2,
"question": "How can I edit my personal details?",
"answer": "You can blah blah blah....."
},
{
"nodeId": 3,
"question": "What is an ABC?",
"answer": "An ABC is where you can....."
}
]
}
一旦检索到iContent转换为JSON?
我认为您应该创建类来定义所需的JSON,然后可以选择新格式。类似:
public class Faq
{
public int nodeId { get; set; }
public string question { get; set; }
public string answer { get; set; }
}
public class FaqCollection
{
public IList<Faq> faqs { get; set; }
}
然后填充对象:
var faqsCollection = new FaqCollection();
faqsCollection.faqs = faqEntities.Select(y => new Faq { nodeId = y.nodeId, question = y.question, answer = y.answer }).ToList();
根据您的需求,您甚至可能不必在此之前转换为JSON。