我正在使用Unity进行渲染的Microsoft Hololens 3D应用程序。我从Web服务和本地文件中获取Json数据以进行某些设置。测试程序并没有给我任何错误,实际上工作得很好,但当我尝试在模拟器或真正的hololens上部署版本时,我得到以下错误。
Exception thrown: 'Newtonsoft.Json.JsonReaderException' in Newtonsoft.Json.dll
JsonReaderException: Error reading JToken from JsonReader. Path '', line 0, position 0.
at Newtonsoft.Json.Linq.JToken.ReadFrom(JsonReader reader, JsonLoadSettings settings)
at Newtonsoft.Json.Linq.JToken.Parse(String json, JsonLoadSettings settings)
at Assets.Scripts.CreateHyrarchy.Start()
at Assets.Scripts.CreateHyrarchy.$Invoke0(Int64 instance, Int64* args)
at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
(Filename: <Unknown> Line: 0)
我认为这与以下几行代码有关。
string data = reader.ReadData(Globals.Globals.RESOURCEFILE);
JToken jObject = JToken.Parse(data);
Globals为我提供了资源路径。
public static String RESOURCEFILE = Application.dataPath+"/Resources/pipeLayout.json";
最后我希望阅读的json文件看起来像这样。
{
"layers":[
{
"type": "water",
"size-multiplier": 2,
"color": "#00f",
"hidden": false
},
{
"type": "gas",
"size-multiplier": 1,
"color": "yellow",
"hidden": false
},
{
"type": "riool",
"size-multiplier": 5,
"color": "#FF0000",
"hidden": false
}
]
}
当我调用一个也返回json字符串的服务时,我遇到了同样的问题。
他们都使用像这样的读者对象。
public String ReadData(String source){
{
//Open a call to a webservice
using (WWW webClient = new WWW(source))
{
//Wait until call is completed
while (!webClient.isDone)
{
new WaitForSeconds(1f);
}
//Get the desired data out
String json = webClient.text.ToString();
return json;
}
}
我尝试按如下方式转换json数据
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>();
}
和Json数据看起来像这样
{
"type": "FeatureCollection",
"totalFeatures": 12,
"features": [
{
"type": "Feature",
"id": "pijpleidingen.3",
"geometry": {
"type": "MultiLineString",
"coordinates": [
[
[
4.4361,
51.1465
],
[
4.4356,
51.1481
]
]
]
},
"geometry_name": "geom",
"properties": {
"type": "gas",
"diepte_cm": 100,
"bbox": [
4.4356,
51.1465,
4.4361,
51.1481
]
}
},
{
"type": "Feature",
"id": "pijpleidingen.8",
"geometry": {
"type": "MultiLineString",
"coordinates": [
[
[
4.4362,
51.1474
],
[
4.436,
51.1477
],
[
4.4361,
51.1477
]
]
]
},
"geometry_name": "geom",
"properties": {
"type": "gas",
"diepte_cm": 100,
"bbox": [
4.436,
51.1474,
4.4362,
51.1477
]
}
}
}
有人能解释我发生了什么吗?