尝试使用反向获取位置名称。我正在使用Unity的JSONutility解析从Google API获得的JSON字符串。信息不会存储在本地对象中,因此我总是会得到nullreference异常。
要检索信息,我正在使用UnityWebRequest类。我将信息写到一个简单的文本文件中,以检查请求是否确实已发送,并且信息被写入了文本文件,但是当我想用它在游戏中创建某些东西时,它不起作用。
我使用http://json2csharp.com/根据我的JSON文件创建了C#类。
我在https://jsonlint.com/处验证了JSON文件
//This is the c# file I created out of the JSON format gotten from google
//It is validated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RootObject
{
public PlusCode plus_code;//This doesn't get created at all
public List<Result> results;//This creates a list of size 11, however the list is always empty and I don't know why.
public string status;//For some reason this is the only variable that gets populated
public static RootObject CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<RootObject>(jsonString);
}
}
public class PlusCode
{
public string compound_code;
public string global_code;
}
public class AddressComponent
{
public string long_name;
public string short_name;
public List<string> types;
}
public class Northeast
{
public double lat;
public double lng;
}
public class Southwest
{
public double lat;
public double lng;
}
public class Bounds
{
public Northeast northeast;
public Southwest southwest;
}
public class Location
{
public double lat;
public double lng;
}
public class Northeast2
{
public double lat;
public double lng;
}
public class Southwest2
{
public double lat;
public double lng;
}
public class Viewport
{
public Northeast2 northeast;
public Southwest2 southwest;
}
public class Geometry
{
public Bounds bounds;
public Location location;
public string location_type;
public Viewport viewport;
}
public class PlusCode2
{
public string compound_code;
public string global_code;
}
public class Result
{
public List<AddressComponent> address_components;
public string formatted_address;
public Geometry geometry;
public string place_id;
public List<string> types;
public PlusCode2 plus_code;
}
这就是我试图从中调用它的地方。
public void ShowStaticMap()
{
//Grab GEO Data
StartCoroutine(FetchGeoData());
newCase.date = DateTime.Today.ToString();
StartCoroutine(GetLocationName());//This runs.
}
IEnumerator GetLocationName()
{
UnityWebRequest www = UnityWebRequest.Get(geoUrl);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
var TestRootObject = RootObject.CreateFromJSON(www.downloadHandler.text).plus_code.compound_code;// This is line 147. Results in a nullreference exception.
/*
If I am to try this line, it will work without a problem.
RootObject.CreateFromJSON(www.downloadHandler.text).status;
*/
Debug.Log(TestRootObject);//There is nothing here.
}
}
NullReferenceException:对象引用未设置为对象的实例 est.UI.CreateCaseScreen + d__19.MoveNext()(位于Assets / Esteban / Scripts / ScreenTypes / CreateCaseScreen.cs:147) UnityEngine.SetupCoroutine.InvokeMoveNext(System.Collections.IEnumerator枚举器,System.IntPtr returnValueAddress)(位于C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)
答案 0 :(得分:2)
好吧...在这里真傻。 对于曾经遇到过类似问题的任何人...创建原始类时(因为没有任何东西可以继承Unity中的任何东西...)请确保将它们声明为[Serializable]
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable] //THIS! On all your classes!!!
public class RootObject
{
public PlusCode plus_code;
public List<Result> results;
public string status;
public static RootObject CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<RootObject>(jsonString);
}
}
[Serializable]
public class PlusCode
{
public string compound_code;
public string global_code;
}
[Serializable]
public class AddressComponent
{
public string long_name;
public string short_name;
public List<string> types;
}
[Serializable]
public class Northeast
{
public double lat;
public double lng;
}
[Serializable]
public class Southwest
{
public double lat;
public double lng;
}
[Serializable]
public class Bounds
{
public Northeast northeast;
public Southwest southwest;
}
[Serializable]
public class Location
{
public double lat;
public double lng;
}
[Serializable]
public class Northeast2
{
public double lat;
public double lng;
}
[Serializable]
public class Southwest2
{
public double lat;
public double lng;
}
[Serializable]
public class Viewport
{
public Northeast2 northeast;
public Southwest2 southwest;
}
[Serializable]
public class Geometry
{
public Bounds bounds;
public Location location;
public string location_type;
public Viewport viewport;
}
[Serializable]
public class PlusCode2
{
public string compound_code;
public string global_code;
}
[Serializable]
public class Result
{
public List<AddressComponent> address_components;
public string formatted_address;
public Geometry geometry;
public string place_id;
public List<string> types;
public PlusCode2 plus_code;
}