簡體   English   中英

string.replace()不能替換char“ \\”“

[英]string.replace() cannot replace char “ \” "

我在MVC模型中的請求帖子下面有json

"{\"Brand\":\"\",\"Name\":\"apotik AA\",\"Desc\":\"\",\"Address\":\"Address\",\"Phone\":\"\",\"Tag\":\"\",\"City\":\"BEKASI\",\"Status\":\"0\"}

我不知道為什么json對象只用(\\")而不是(")填充,但是當我用html allert檢查值時,它不滿足(\\")

所以我想用(/")代替(/") (")

[Authorization()]
[HttpPost()]
public ActionResult Update(VModel vModel)
{
   string JsonArray = (vModel.JsonVar).Replace('\"',' ');
}

但結果仍然

"{\"Brand\":\"\",\"Name\":\"apotik AA\",\"Desc\":\"\",\"Address\":\"Address\",\"Phone\":\"\",\"Tag\":\"\",\"City\":\"BEKASI\",\"Status\":\"0\"}

怎么能?

可能是您將VS調試器的輸出與真實的字符串弄錯了。 嘗試在調試器中打開實際的字符串(按loupe)。 但是,如果您實際上需要將\\“替換為”,請使用以下代碼:

string JsonArray = JsonVar.Replace("\\\"", "\"");

或者,您可以使用刪除所有斜杠

string JsonArray = JsonVar.Replace("\\", string.Empty);
class model
{
    public static string jsonVar { get; set; }
}

   public static string ReplaceStringChars(string str)
    {
        string newStr = string.Empty;

        newStr = str.Replace('\"' , ' ');

        return newStr;
    }

    static void Main(string[] args)
    {
        model.jsonVar = "{\"Brand\":\"\",\"Name\":\"apotik AA\",\"Desc\":\"\",\"Address\":\"Address\",\"Phone\":\"\",\"Tag\":\"\",\"City\":\"BEKASI\",\"Status\":\"0\"}";
        ReplaceStringChars(model.jsonVar);
    }

結果:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM