0

I am currently implementing a system and came across the following problem.

We are making a call to a 3rd party supplier that provides a string of contents which is not in JSON format, therefore we are trying to remove content from the string, in order to build a JSON string and serialize it into our own object.

We are trying to remove the part from {"imgCount to ]", (just before the "images":

An example of the string is the following:

img_CB("imgCount":31,"imgHash":"[6ede94341e1ba423ccc2d4cfd27b9760]","images":{...});

The issue is that, the imgCount and imgHash may not be in that order. It could be in something like the following:

img_CB("images":{....}, "imgHash":"[6ede94341e1ba423ccc2d4cfd27b9760]", "imgCount":31);

Therefore this makes it quite dynamic and hard to determine where to start "replacing" from.

Would anyone help to possibly build a regex expression to replace/remove the imgHash and imgCount tags with their values please?

Thanks

M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
Frank
  • 19
  • 8

2 Answers2

0

Looks like you're getting a jsonp response. Have a look at this answer on how to parse your json after stripping off the jsonp stuff:

How to parse JSONP using JSON.NET?

Example:

string supposedToBeJson = "img_CB(\"imgCount\":31,\"imgHash\":\"[6ede94341e1ba423ccc2d4cfd27b9760]\",\"images\":{});";

var jobject = JObject.Parse(supposedToBeJson.Replace("img_CB(", "{").Replace(");", "}"));

var images = jobject.SelectToken("images");
Community
  • 1
  • 1
Lars Anundskås
  • 1,345
  • 10
  • 24
0

try this:

str = Regex.Replace(str, "\"imgCount*,","");
str = Regex.Replace(str, "\"imgHash*,","");
str = Regex.Replace(str, ",*\"imgCount*)",")");
str = Regex.Replace(str, ",*\"imgHash*)",")");
eladzaa
  • 168
  • 1
  • 2
  • 9