在使用Litjson解析json数据时,出现了乱码问题,
错误提示如下:
JsonException: Invalid character '' in input string
LitJson.Lexer.NextToken ()
LitJson.JsonReader.ReadToken ()
LitJson.JsonReader.Read ()
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader)
LitJson.JsonMapper.ToObject[JsonData] (System.String json)
我是使用WWW加载到json文件,然后进行转换成实体类
WWW www = new WWW(path);//加载Json文件
if(www.isDone)
{
string jsonString = WWW.text;
JsonData = JsonMapper.ToObject<JsonData>(jsonString);//解析
}
在转换成JsonData具体的实体类时,发生了上面的错误提示(很头痛)。
首先,数据是解析到的,只是进行转换的过程中,发生了乱码问题,进行了很多尝试,后来发现一种解决问题的方法:
System.Text.UTF8Encoding.GetString(Byte[], Int32, Int32)
首先我们来认识一下这个编码转换的函数:
Type: System.Byte[]
包含要解码的字节序列的字节数组。
Type: System.Int32
第一个要解码的字节的索引。
Type: System.Int32
要解码的字节数。
我们将上述的代码进行稍微的修改就可以了:
WWW www = new WWW(path);//加载Json文件
if(www.isDone)
{
string jsonString = EncodingUTF8.GetString(www.bytes,3,www.bytes.length-3);
JsonData = JsonMapper.ToObject<JsonData>(jsonString);//解析
}
这样就没有什么问题了。