将字符串反序列化为JSON对象时出错

编程入门 行业动态 更新时间:2024-10-27 21:15:45
本文介绍了将字符串反序列化为JSON对象时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个php文件,该文件读取.txt文件并将其通过php服务器发送到c#统一脚本.下面是显示前三行的文本文件的片段:

I have a php file which reads a .txt file and sends it via a php server to a c# unity script. Below is a snippet of the text file showing the first 3 lines:

{ "lemma" : "aljotta", "gloss" : "Fisħ soup" } { "lemma" : "arguzin", "gloss" : "Slave driver" } { "lemma" : "armunjaka", "gloss" : "Armunjaka" }

这是php脚本:

<?php $file = fopen("lemmas.txt", "r"); echo fread($file, filesize("lemmas.txt")); fclose($file); ?>

在c#脚本中,返回文本,并将每一行分成一个数组(字符串[]行)插槽,如下所示:

In a c# script, the text is returned and each line is separated into an array (string[] lines) slot as seen below:

IEnumerator GetTextFromFile() { bool succcessful = true; WWWForm form = new WWWForm(); WWW www = new WWW("localhost:9000/tounity.php", form); yield return www; if(www.error != null) { succcessful = false; } else { succcessful = true; } if (succcessful) { populateWordList(www.text); } } void populateWordList(string text) { string[] textArray = text.Split('\n'); wordsList = gameDatabase.GetWords(textArray); }

然后将数组传递给一个方法,该方法将每行反序列化为GameDatabase类的对象,如下图所示:

The array is then passed to a method which deserializes each line into an object of class GameDatabase as seen in the image below:

public string lemma { get; set; } public string gloss { get; set; } public GameDatabase(string lemma, string gloss) { this.lemma = lemma; this.gloss = gloss; } public ArrayList GetWords(string[] lines) { foreach (string line in lines) { GameDatabase gd = JsonConvert.DeserializeObject<GameDatabase>(line); lemmasAndGlossesList.Add(new GameDatabase(gd.lemma, gd.gloss)); } foreach(GameDatabase line in lemmasAndGlossesList) { Debug.Log(line.lemma + "------" + line.gloss); } return lemmasAndGlossesList; }

该错误发生在GameDatabase gd = JsonConvert.DeserializeObject<GameDatabase>(line);中,并返回

The error occurs in GameDatabase gd = JsonConvert.DeserializeObject<GameDatabase>(line); and returns

JsonReaderException: Unexpected character encountered while parsing value: . Path '', line 0, position 0.

我进行了广泛的搜索,但是没有找到任何有效的方法.任何帮助将不胜感激.值得注意的是,在不使用php的情况下将文本文件直接加载为unity时不会发生此问题.

I have searched extensively, however, haven't found anything that works. Any help would be greatly appreciated. It is worth noting that this problem doesn't happen when loading the text file directly into unity without using php.

编辑

使用vs调试器时,这是要反序列化的行中的值:

When using the vs debugger this is the value in the line to be deserialized:

但是Visual Studio 2019的JSON可视化工具报告此:

The JSON visualiser of Visual Studio 2019 however reports this:

推荐答案

感谢Jonathon K的评论,您的回复我们可以看到PHP脚本返回的数据开始带有BOM :前三个字节. 这篇不错的文章解释了如何正确处理此类数据.简而言之:使用StreamReader读取数据.

Thanks to Jonathon K's comment and your reply we can see the data returned by the PHP script starts with a BOM: the first three bytes. This nice article explains how to handle such data properly. In short: use a StreamReader to read the data.

这个小程序演示了如何处理您的数据:

This little program demonstrates how it could work with your data:

using System; using Newtonsoft.Json; using System.IO; public class Program { public static void Main() { var bytes = new byte[] { 0xEF,0xBB,0xBF,0x7B,0x20,0x22,0x6C,0x65,0x6D,0x6D,0x61,0x22, 0x20,0x3A,0x20,0x22,0x61,0x72,0x67,0x75,0x7A,0x69,0x6E,0x22, 0x2C,0x20,0x22,0x67,0x6C,0x6F,0x73,0x73,0x22,0x20,0x3A,0x20, 0x22,0x53,0x6C,0x61,0x76,0x65,0x20,0x64,0x72,0x69,0x76,0x65, 0x72,0x22,0x20,0x7D}; string json; using(var ms = new MemoryStream(bytes)) using(var sr = new StreamReader(ms)) { json = sr.ReadToEnd(); Console.WriteLine(json); } // I'm using dynamic here. In your case you can use GameDatabase dynamic obj = JsonConvert.DeserializeObject(json); Console.WriteLine(obj.lemma); } }

输出:

{ "lemma" : "arguzin", "gloss" : "Slave driver" } arguzin

更多推荐

将字符串反序列化为JSON对象时出错

本文发布于:2023-11-17 07:44:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609345.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   序列   对象   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!