-1

I am quite new to Regular expressions. I want to filter out a Http GetRequest string and return the tokens as a Dictionary object so I can find each token. For example, the string returned is as follows

"{\"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-302c02147c32c6cad7f273be1c06cc7377a22520ce4d31af0214541d250968177ad25a4041ef9c039accca483132@api.test.netbanx.com/hosted/v1/orders/29BKR08JSRJ5BE11LD/resend_callback\"}],\"merchantRefNum\":\"fd63cb9d-b586-664d-9bba-8492baf4bad9\",\"mode\":\"live\",\"totalAmount\":\"100\",\"type\":\"order\"}"

I want to return the currencyCode as the Key and CAD as the Value. Same goes for the link, rel => hosted_payment, uri => https://.....

I am using the following but it is not what I want

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();

Thanks in advance

Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
Zukunft
  • 17
  • 1
  • 5

2 Answers2

2

I am agree with commentar from Jeroen Vannevel, you could simply parse your json with Json.net:

var parsed = JObject.Parse(orderResp);
var code = parsed["currencyCode"];
Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
1
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-302c02147c32c6cad7f273be1c06cc7377a22520ce4d31af0214541d250968177ad25a4041ef9c039accca483132@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;       
    }
}
Duncan Carr
  • 250
  • 1
  • 5