我正在尝试使用
从umbraco中的节点获取图像var image = node.GetProperty("postImage").Value;
然而我正在
"{\r\n \"focalPoint\": {\r\n \"left\": 0.5,\r\n \"top\": 0.5\r\n },\r\n \"src\": \"/media/8354/Image123Test.jpg\",\r\n \"crops\": [\r\n {\r\n \"alias\": \"blogPost\",\r\n \"width\": 200,\r\n \"height\": 200\r\n },\r\n {\r\n \"alias\": \"thumbnail\",\r\n \"width\": 50,\r\n \"height\": 50\r\n },\r\n {\r\n \"alias\": \"featuredImage\",\r\n \"width\": 320,\r\n \"height\": 238\r\n }\r\n ]\r\n}"
返回。
是否有可能提出只返回“/media/8354/Image123Test.jpg”的正则表达式?
感谢,
答案 0 :(得分:3)
您似乎正在使用Umbraco中的图像裁剪器数据类型。你不应该自己解析JSON,Umbraco为此建立了实用程序。
尝试(在Umbraco 7.3.5 +中):
// Where "node" is IPublishedContent.
// "blogpost" is the alias of your crop setting.
// "Url" is of type System.Web.Mvc.UrlHelper
IHtmlString imageUrl = Url.GetCropUrl(node, "postImage", "blogpost");
这将获得您在Umbraco中为该裁剪设置别名指定的宽度,高度和居中的图像。
.GetCropUrl
方法中还有一些重载,可让您在代码中指定宽度和高度。
查看文档here。
答案 1 :(得分:1)
根据媒体项目,umbracoFile可以存储为JSON或仅存储路径。出于这个原因,我创建了这两个小方法,以确保我始终获得文件路径。
private static string GetServerFilePath(IMedia mediaItem, bool isNew)
{
string filePath = (string)mediaItem.Properties["umbracoFile"].Value;
if (!isNew || filePath.Contains("{"))
{
filePath = GetExistingFilePath(filePath);
}
return HttpContext.Current.Server.MapPath(filePath);
}
private static string GetExistingFilePath(string filePath)
{
var jsonFileDetails = JObject.Parse(filePath);
string src = jsonFileDetails["src"].ToString();
filePath = src;
return filePath;
}
答案 2 :(得分:0)
你可以尝试这样的事情。它会搜索\"src\": \"
并将所有内容都移到下一个\"
string test = "{\r\n \"focalPoint\": {\r\n \"left\": 0.5,\r\n \"top\": 0.5\r\n },\r\n \"src\": \"/media/8354/Image123Test.jpg\",\r\n \"crops\": [\r\n {\r\n \"alias\": \"blogPost\",\r\n \"width\": 200,\r\n \"height\": 200\r\n },\r\n {\r\n \"alias\": \"thumbnail\",\r\n \"width\": 50,\r\n \"height\": 50\r\n },\r\n {\r\n \"alias\": \"featuredImage\",\r\n \"width\": 320,\r\n \"height\": 238\r\n }\r\n ]\r\n}";
string regx = "(?<=\\\"src\\\": \\\").+(?=\")";
System.Text.RegularExpressions.Match mat = System.Text.RegularExpressions.Regex.Match(test, regx);
答案 3 :(得分:0)
它看起来像个json。因此,您可以将此字符串解析为Json并从键“src”中检索值。例如,您可以使用Newtonsoft.Json
using Newtonsoft.Json.Linq;
...
string json = node.GetProperty("postImage").Value;
var parsedJson = JObject.Parse(json);
var srcValue = parsedJson.Properties().FirstOrDefault(item => item.Name == "src")?.Value;