2016-12-29 6 views
-1

正規表現はかなり新しくなっています。私はHttpのGetRequest文字列をフィルタリングし、各トークンを見つけることができるようにDictionaryオブジェクトとしてトークンを返したいと思います。正規表現とLINQを使用してCで辞書オブジェクトを返す方法#

"{\" CURRENCYCODE \は、例えば、以下のように返される文字列は "\" のCAD \」\ "のID \":\ "29BKR08JSRJ5BE11LD \" \ "リンク\":[{\ "rel \":\ "hosted_pa​​yment \"、\ "uri \":\ "https://pay.test.netbanx.com/hosted/v1/payment/53616c7465645f5fcb770fe4ad39262c27ae2fba4189e1ef89aacc3fac4e0fc0ba7d1699c4a50672 \"}、\ "rel \":\ "self \"、\ "uri \":\ "https ... \ ""、 "" \ "再販\"、\ "再販\"、\ "ウリ\":\ "https://37520-1001043850:B-qa2-0-56797b63-0-302c02147c32c6cad7f273be1c06c[email protected]api.test.netbanx.com/hosted/v1/orders/29BKR08JSRJ5BE11LD/resend_callback \"}]、\ "merchantRefNum \":\ "fd63cb9d-b586-664d-9bba-8492baf4bad9 \ \ "モード\":\ "ライブ\"、​​\ "合計サイズ\":\ "100 \"、\ "タイプ\":\ "注文\"} "

私はcurrencyCodeをキーと値としてのCAD。同じことが、リンク、REL => hosted_pa​​yment、URI => HTTPSのために行く:// .....

私は、次を使用していますが、それは私が事前に

Regex regex = new Regex(@"((""((?<token>.*?)(?<!\\)"")|(?<token>[\w]+))(\s)*)", 
     RegexOptions.None); 

return (from Match m in regex.Matches(orderResp) 
    where m.Groups["token"].Success 
    select m.Groups["token"].Value).ToArray(); 

おかげで欲しいものではありません

+7

ローJSONのようなoks。なぜあなたはそれを解析しませんか? –

+0

ありがとうございます。私は私の答えを得た – Zukunft

答えて

2

私はイェルーンVannevelからcommentarに同意しています、あなたは単にJson.netであなたのJSONを解析することができます:

var parsed = JObject.Parse(orderResp); 
var code = parsed["currencyCode"]; 
0
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Web.Script.Serialization; 

namespace Chinenye 
{ 
    class Program 
    { 
     static void Main() 
     { 
      var objectString = "{\"currencyCode\":\"CAD\",\"id\":\"29BKR08JSRJ5BE11LD\",\"link\":[{\"rel\":\"hosted_payment\",\"uri\":\"https://pay.test.netbanx.com/hosted/v1/payment/53616c7465645f5fcb770fe4ad39262c27ae2fba4189e1ef89aacc3fac4e0fc0ba7d1699c4a50672\"},{\"rel\":\"self\",\"uri\":\"https...\"},{\"rel\":\"resend_callback\",\"uri\":\"https://37520-1001043850:B-qa2-0-56797b63-0-302c02147c32c6cad7f273be1c06c[email protected]api.test.netbanx.com/hosted/v1/orders/29BKR08JSRJ5BE11LD/resend_callback\"}],\"merchantRefNum\":\"fd63cb9d-b586-664d-9bba-8492baf4bad9\",\"mode\":\"live\",\"totalAmount\":\"100\",\"type\":\"order\"}"; 

      JavaScriptSerializer serializer = new JavaScriptSerializer(); 

      var o = serializer.Deserialize<HttpObject>(objectString); 

      Debugger.Break(); 
     } 
    } 

    class HttpObject 
    { 
     public string currencyCode; 
     public string id; 
     public List<Link> link; 
     public string merchantRefNum; 
     public string mode; 
     public string totalAmount; 
     public string type; 
    } 

    class Link 
    { 
     public string rel; 
     public string uri;  
    } 
} 
+0

ありがとう。これは間違いなく助けになりました:) – Zukunft

関連する問題