Unity使用Visual Stuido获取Json字符串自带双引号的处理

博主在做Unity使用Socket通讯的登录注册模块时遇到了这个问题,后端返回Json数据如下:

C#核心代码如下:

IEnumerator LoginRequest(string url)
    {

        using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
        {
           // Debug.Log("GetSuccess");
            yield return webRequest.SendWebRequest();
            if (!string.IsNullOrEmpty(webRequest.error))
            {
                Debug.LogError(webRequest.error);
            }
            else
            {
                reminderText.text = webRequest.downloadHandler.text;
                string resultStr = Regex.Unescape(reminderText.text).Trim('"');
                Debug.Log("C#原生字符串"+"loginSuccess");
                Debug.Log("获取到的字符串" + reminderText.text);
                Debug.Log("处理后的字符串"+resultStr);
                if (string.Compare(resultStr, "loginSuccess") == 0)
                {
                    Debug.Log("1");
                    hallSetUI.SetActive(true);
                    loginUI.SetActive(false);
                    SceneManager.LoadScene(0);
                }
                else if(string.Compare(resultStr, "passwordError") == 0)
                {

                }
            }
        }
    }

Unity控制台对应输出如下:

 问题所在就是获取到的字符串比我们自己定义的字符串多了个双引号,相对应的处理方法也在代码中,为方便大家观看,我将关键代码单独放出来,如下:

string resultStr = Regex.Unescape(reminderText.text).Trim('"');

其中resultStr为我们定义的用来存放结果的字符串,reminderText.text为需要处理的字符串,Trim中的单引号里的字符为待处理字符串中要消除的字符。

猜你喜欢

转载自blog.csdn.net/qq_56755504/article/details/131708216