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" }
This is the php script:
<?php
$file = fopen("lemmas.txt", "r");
echo fread($file, filesize("lemmas.txt"));
fclose($file);
?>
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("http://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);
}
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;
}
The error occurs in GameDatabase gd = JsonConvert.DeserializeObject<GameDatabase>(line);
and returns
JsonReaderException: Unexpected character encountered while parsing value: . Path '', line 0, position 0.
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.
EDIT
When using the vs debugger this is the value in the line to be deserialized:
The JSON visualiser of Visual Studio 2019 however reports this: