我對正則運算式有疑問。該變數media.Value可能包含字符“() ”,這會導致正則運算式出錯并阻止其作業。
我的代碼
Match renderMatch = Regex.Match(mediaMatch.Groups[0].Value, "(?<=\"name\":\"" media.Value "\",\"render\":).*?(?=,)");
Match mutedMatch = Regex.Match(mediaMatch.Groups[0].Value, "(?<=\"muted\":).*?(?=,\"name\":\"" media.Value "\")");
我使用的 json
[{"alignment":5,"cx":844.0,"cy":264.0,"id":4,"locked":false,"muted":false,"name":"Text (GDI )","render":true,"source_cx":844,"source_cy":264,"type":"text_gdiplus_v2","volume":1.0,"x":549.0,"y":383.0},{"alignment":5,"cx":1920.0,"cy":1080.0,"id":3,"locked":false,"muted":false,"name":"Color","render":true,"source_cx":1920,"source_cy":1080,"type":"color_source_v3","volume":1.0,"x":0.0,"y":0.0}]
只要名稱欄位中沒有“()”,一切正常。例如:
在職的
"muted":false,"name":"Color","render":true
不作業
"muted":false,"name":"Text (GDI )","render":true
問題是。是否有任何正則運算式選項會忽略字串中的 (),或者我怎么能得到這樣的輸出:
"Text \(GDI \ \)"
uj5u.com熱心網友回復:
您可以將JSON字串反序列化為強型別模型,然后收集所需欄位
您的 JSON 字串的示例是:https ://dotnetfiddle.net/cqkOdu
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
var myJsonResponse= @"[{'alignment':5,'cx':844.0,'cy':264.0,'id':4,'locked':false,'muted':false,'name':'Text (GDI )','render':true,'source_cx':844,'source_cy':264,'type':'text_gdiplus_v2','volume':1.0,'x':549.0,'y':383.0},{'alignment':5,'cx':1920.0,'cy':1080.0,'id':3,'locked':false,'muted':false,'name':'Color','render':true,'source_cx':1920,'source_cy':1080,'type':'color_source_v3','volume':1.0,'x':0.0,'y':0.0}]";
List<Root> myDeserializedClass = JsonConvert.DeserializeObject<List<Root>>(myJsonResponse);
foreach(var item in myDeserializedClass)
{
Console.WriteLine(item.name);
}
}
}
public class Root
{
public int alignment { get; set; }
public double cx { get; set; }
public double cy { get; set; }
public int id { get; set; }
public bool locked { get; set; }
public bool muted { get; set; }
public string name { get; set; }
public bool render { get; set; }
public int source_cx { get; set; }
public int source_cy { get; set; }
public string type { get; set; }
public double volume { get; set; }
public double x { get; set; }
public double y { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/421909.html
標籤:
下一篇:分隔CSV字串的字母和浮點數
