Huma Ali Huma Ali - 3 months ago 14
JSON Question

Append text in json response

I am receiving following message in JSON format in my variable

responseString


{
"CreateCustomerResponse": {
"ServiceContextType": {
"Status": {
"Code": "EI7",
"MessageType": "ERROR",
"Message": "Email Oops! Looks like you've already created an account. Please <a href='/account'>click here to sign in.</a>"
}
}
}
}


I want to replace the received
href
value with
https://www.myweb.com/signin/
. Is it possible?

string responseString = client.POST(relativeURL, createCustomerRequest, contentType);
responseString = //Replace /account with `https://www.myweb.com/signin/`

C4u C4u
Answer

You can do it using regex like this:

string responseString = client.POST(relativeURL, createCustomerRequest, contentType);
string replace = "https://www.myweb.com/signin/";

string result = Regex.Replace(responseString, @"href='(.*?)'", $"href='{replace}'");

// => ...href='https://www.myweb.com/signin/'>click here to sign in.</a>...
Comments