我有18个名为<条码>的JsonFileAsset的JSON档案,是具有以下特征的Trie结构的典型词典:
- "@" represents the end of a string.
- "#" represents children nodes in the Trie.
- "$" represents true , indicating the end of a word.
- "%" represents false , indicating not the end of a word."
在JSON档案中:
{"@":"%","#":{"a":{"@":"%","#":{"a":{"@":"%","#":{"h":{"@":"$","#":{"e":{"@":"%","#":{"d": ...
Here is the code that I tried to make the deserialization process work on TrieHandler
:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
public class TrieHandler
{
private Dictionary<string, object> trieDict;
public void LoadTrieFromJson(string jsonContent)
{
trieDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonContent);
if (trieDict == null)
{
Debug.LogError("Failed to parse JSON content: " + jsonContent);
}
Debug.Log("TrieDict size: " + trieDict.Count);
}
public bool IsWordValid(string word)
{
return IsWordInTrie(trieDict, word, 0);
}
private bool IsWordInTrie(Dictionary<string, object> node, string word, int index)
{
if (index == word.Length)
{
// Check if the current node represents the end of a word
return node.ContainsKey("@");
}
char currentChar = word[index];
if (!node.ContainsKey("#"))
return false; // No children, word does not exist
Dictionary<string, object> childrenDict = (Dictionary<string, object>)node["#"];
if (!childrenDict.ContainsKey(currentChar.ToString()))
return false; // Current character is not present in children
return IsWordInTrie(childrenDict[currentChar.ToString()] as Dictionary<string, object>, word, index + 1);
}
}
public class GameplayController : MonoBehaviour
{
public string selectedTileLetters = "";
// Trie handler instance
private TrieHandler trieHandler;
private string jsonContent;
void Start()
{
trieHandler = new TrieHandler();
TextAsset jsonFile = Resources.Load<TextAsset>("JsonFileAsset");
if (jsonFile != null)
{
trieHandler.LoadTrieFromJson(jsonFile.text);
}
else
{
Debug.LogError("Failed to load JSON file");
}
}
... // Rest of the code not shown
}
我试图利用“团结”的本地Json通用工具,将Json资产储存在资源名录上。 trieDict = JsonUtility.FromJson<Dictionary<string, Object>>jsonContent)
,但trieDict.Count
向我扔下了一张<0。 如果我尝试使用<代码>纽顿软。 Json s JsonConvert.DeserializeObject
然后,我拿到<代码>trieDict.Count>>。 然而,由于我的<代码>JsonFileAsset文档中含有400k+关键值的配对器,并且<代码>trieDict.Count应当印制更多份。 在该方案的其他部分,IsordValid
方法从来未退回Tru
boolean,我已经测试了我的Trie 读物算法,并做了完全的细微工作,使问题仅与分离。 LoadTrieFromJson
sfally deserialization process.
我应当做些什么来确定<代码>的脱硫过程。 LoadTrieFromJson with Newtonsoft.Json
?