unity读取接口json出现了JsonException: Invalid character ‘ ‘ in input string这个问题

unity读取接口json出现了JsonException: Invalid character ' ' in input string这个问题


这个原因是后端给的json的编码有问题

老方法

保存到本地在读取。

IEnumerator loadjson()
    {
    
    
        WWW www = new WWW ("http://www.wotimadeyebuzhidudizhishisha.com");
        yield return www;
    File.WriteAllText(Application.dataPath+"/json.txt",www.text);//保存本地

        StreamReader sr=File.OpenText(Application.dataPath+"/json.txt");//读取

        Root josn = LitJson.JsonMapper.ToObject<</span>Root>(sr.ReadLine());//解析

        Debug.Log(josn.result.title);

    }

本地C#直接改

www的

IEnumerator Starts()
    {
    
    
        WWW www = new WWW("http://www.wotimadeyebuzhidudizhishisha.com");
        yield return www;
        Debug.Log(www.text);
        LitJson.JsonData data = LitJson.JsonMapper.ToObject(System.Text.Encoding.UTF8.GetString(www.bytes, 3, www.bytes.Length - 3));

        Debug.Log(data.ToJson());
        Debug.Log(data["data"][0].ToJson());
    }

UnityWebRequest的

IEnumerator GetData()
    {
    
    

        UnityWebRequest request = UnityWebRequest.Get("http://www.wotimadeyebuzhidudizhishisha.com");
        yield return request.SendWebRequest();
        if (request.isNetworkError || request.isHttpError)
        {
    
    
            Debug.Log(request.error);
        }
        else
        {
    
    
            Debug.Log(request.downloadHandler.text);
            JsonData objs = JsonMapper.ToObject(System.Text.Encoding.UTF8.GetString(request.downloadHandler.data, 3, request.downloadHandler.data.Length - 3));
            Debug.Log(objs.ToJson());
        }
    }

猜你喜欢

转载自blog.csdn.net/bgp_halt_here/article/details/118085093