-1

I have a string like :

"{
\"__type\":\"DailyRequestItem:#CapitalIQ.DataAPI.Download\",
\"Identifier\":{\"__type\":\"CompanyIdentifier:#CapitalIQ.DataAPI\",\"SearchString\":\"goog\"},
\"FormulaMetric\":{\"MetricName\":\"IQ_LASTSALEPRICE\",\"MetricId\":0},
\"ReturnType\":0,
\"CurrencyConversionInfo\":{\"CurrencyId\":160,\"CurrencyConversionMode\":0},\"Response\":{
\"Id\":0,\"CompanyId\":29096,\"TradingItemId\":11311662,\"RequestedCurrencyId\":0,\"CurrencyConversionModeId\":0,\"Error\":0,\"Values\":[{\"CurrencyConversionDate\":\"\\/Date(-62135578800000-0500)\\/\",\"DataTypeId\":2,\"ReportedCurrencyId\":0,\"Id\":0,\"CurrencyId\":160,\"ScaleId\":0,\"UnitsId\":0,\"ValueId\":0,\"ValueAsString\":\"896.598000\",\"Delimiter\":\",\",\"SubValueList\":null,\"Error\":0,\"ClickThroughTypeId\":0,\"InstanceId\":0,\"PricingDate\":\"\\/Date(1374724800000-0400)\\/\",\"ConversionMode\":0,\"AuditabilityTypeId\":0,\"AsOfDate\":\"\\/Date(1374724800000-0400)\\/\",\"CompanyId\":0,\"DataItemId\":0,\"TradingItemId\":0}]
},
\"RangeInfo\":{\"AsOfDate\":\"\\/Date(1374724800000-0400)\\/\",\"StartDate\":\"\\/Date(-62135596800000)\\/\",\"EndDate\":\"\\/Date(-62135596800000)\\/\",\"Frequency\":1,\"ReturnType\":0,\"TradingDayOffset\":null,\"IsIntraday\":false},\"Period\":null}"

from which I need to remove this part:

\"Response\":{
\"Id\":0,\"CompanyId\":29096,\"TradingItemId\":11311662,\"RequestedCurrencyId\":0,\"CurrencyConversionModeId\":0,\"Error\":0,\"Values\":[{\"CurrencyConversionDate\":\"\\/Date(-62135578800000-0500)\\/\",\"DataTypeId\":2,\"ReportedCurrencyId\":0,\"Id\":0,\"CurrencyId\":160,\"ScaleId\":0,\"UnitsId\":0,\"ValueId\":0,\"ValueAsString\":\"896.598000\",\"Delimiter\":\",\",\"SubValueList\":null,\"Error\":0,\"ClickThroughTypeId\":0,\"InstanceId\":0,\"PricingDate\":\"\\/Date(1374724800000-0400)\\/\",\"ConversionMode\":0,\"AuditabilityTypeId\":0,\"AsOfDate\":\"\\/Date(1374724800000-0400)\\/\",\"CompanyId\":0,\"DataItemId\":0,\"TradingItemId\":0}]
},    

and serialize it. I'm using this regex but to no avail:

string pattern = @"\\Response\.\]}";

Can someone help me out to form the regex pattern please? Please help

TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47
user1318369
  • 715
  • 5
  • 15
  • 34
  • yes, it's a json string – user1318369 Aug 01 '13 at 01:58
  • 6
    Are you able to use a JSON library to parse it and extract the part you need? – TheEvilPenguin Aug 01 '13 at 02:02
  • I was planning on using Regex, still trying on that..not sure if I got your question, but I am using JSON serializer to parse it into a Request item, which has a Response(child) element..but the problem is the Response item has this XmlIgnore in its data contract, so once I serialize the json string and do Request.Response = null, the Response is still there...have no isea how to progress :( – user1318369 Aug 01 '13 at 02:06
  • 4
    Why in the world would you be trying to parse JSON with a regex when there are much better solutions? – Ken White Aug 01 '13 at 02:06
  • 1
    much better solns like? – user1318369 Aug 01 '13 at 02:07
  • 3
    A quick search here using `[c#] parse JSON` returned [this](http://stackoverflow.com/q/1212344/62576) as the first result. You should learn to search (here or via Google). :-) – Ken White Aug 01 '13 at 02:08
  • 5
    Not very serious: I'm afraid that helping you to come up with regular expression dealing with nested braces/quotes and like can be considered intention to harm... Please save us from helping you to hurt yourself and use existing JSON parser like JSON.Net. – Alexei Levenkov Aug 01 '13 at 02:11
  • Deserialize JSON -> dynamic, profit. – Pete Garafano Aug 01 '13 at 02:23
  • For more on the perils of using regexes for this stuff, [see this classic answer](http://stackoverflow.com/a/1732454/1053021). – michaelb958--GoFundMonica Aug 01 '13 at 03:02

1 Answers1

1

As people suggest to parse a JSON response regex is not an appropriate solution, but still if you do not find any other solution you can use this pattern to resolve your issue:

(\"Response\")[\S\s]*?(?=\"RangeInfo\")

Have a look at this rubular demo.

NOTE:

I expect the JSON response will be always in a specific format and RangeInfo element will be the next upcoming node in the JSON response.

Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56