在json响应中附加文本

时间:2016-08-17 10:05:20

标签: c# json append

我在变量responseString

中收到JSON格式的以下消息
{
  "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>"
      }
    }
  }
}

我想将收到的href值替换为https://www.myweb.com/signin/。有可能吗?

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

2 个答案:

答案 0 :(得分:1)

您可以使用String函数replace(),replaceAll()等。 就像这样:

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

答案 1 :(得分:0)

你可以使用这样的正则表达式来实现:

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>...