c# - parse() 处的 JsonReaderException - HololensDeploy

标签 c# unity3d hololens

我在全息镜头上部署时遇到异常。我不知道为什么或如何解决它。它发生在下面的类中。

class JsonConverter
{
    public Dictionary<GameObject, String> convertJsonToObject(String json) {
        Dictionary<GameObject, String> objectList = new Dictionary<GameObject, string>(); 

        //Change JSON String to JSON Object
        JObject jObject = JObject.Parse(json);

        //Open features array in the object
        foreach(JObject pipe in jObject["features"].ToArray())
        {
            //Parse JSON Object to Creator
            PipeLineCreator p = pipe.ToObject<PipeLineCreator>();
            objectList.Add(p.CreatePipes(),p.Type);
        }
        return objectList;
    }    
}

它要么无法解析我收到的数据,要么我根本得不到任何数据。对于泡沫,我认为问题在于以下类(class)。

class JsonReader
{    
    public String data { get; set; }
    public bool loaded { get; set; }

    public IEnumerator ReadData(String source) {
        using (WWW webClient = new WWW(source))
        {
            loaded = false;
            yield return webClient;
            data =  webClient.text;
            loaded = true;
        }
    }   
}

它尝试从返回 Json 的远程 Web 服务接收数据。程序会等到调用完成后再继续。

//reader is an instance of JsonReader
StartCoroutine(reader.ReadData(Globals.Globals.DATAURL));
yield return new WaitUntil(() => reader.loaded);

有人知道我做错了什么以及我该如何解决这个问题吗?

最佳答案

我解决了我自己的问题。

我的问题是我没有从我的 WWW 调用中收到任何数据。 显然 WWW 类用于接收 HTML 文件,但没有别的(或至少在全息镜头上)。

相反,我使用了 UnityWebRequest 类,它允许您按如下方式从调用中接收字节。

public IEnumerator ReadData(String source) {
        using (UnityWebRequest webClient = UnityWebRequest.Get(source))
        {
            loaded = false;
            yield return webClient.SendWebRequest();
            byte[] bytes = webClient.downloadHandler.data;
            data = Encoding.UTF8.GetString(bytes);
            loaded = true;
            if (data == null||data.Equals(""))
            {
                throw new ArgumentNullException("Data", "No Data recieved from service");
            }
        }

    }

要从字节数组中获取 Json 数据,您只需使用 UTF-8 编码器对其进行编码。

我希望这对其他人也有帮助。

关于c# - parse() 处的 JsonReaderException - HololensDeploy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50003206/

相关文章:

c# - Unity3D 中的 Time.time vs DateTime.UtcNow.Millisecond 性能?

ios - Unity 中的 Azure 空间 anchor 子对象

c# - 如何在 C# 中定义全息坐标系?

c# - 返回新的 LINQ 对象

C#, "Object reference not set to an instance of an object."错误

c# - 避免使用实体类内的方法定义进行查询时出现查询客户端评估错误

c# - 在 HoloLens 1 上,在使用默认构造函数 "ArgumentException: Value does not fall within the expected range"创建 TcpClient 对象时抛出

c# - 验证绑定(bind)到同一对象的不同属性的控件会重置对象值

node.js - 将 Unity3d 与 Node.js 连接起来

c# - 按名称、标签或图层查找不活动的游戏对象