c# - unity Json 字符串导致空引用异常
问题描述
尝试使用反向获取位置名称。我正在使用 Unity 的 JSONutility 来解析从 google API 获得的 JSON 字符串。信息不会存储在我的本地对象中,所以我总是得到一个空引用异常。
为了检索我使用 UnityWebRequest 类的信息。我将信息写入一个简单的文本文件以检查请求是否实际被发送,并且信息被写入文本文件,但是当我想用它在游戏中创建一些东西时,它不起作用。
我使用http://json2csharp.com/根据我的 JSON 文件创建了我的 C# 类。
我在这里验证了 JSON 文件https://jsonlint.com/
//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)
解决方案
好吧...这里真的很傻。对于遇到类似问题的任何人...当您创建原始类时(就像没有从 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;
}
推荐阅读
- web-scraping - 将表格从网络导入到谷歌表格
- javascript - 如果键已经存在,map.set() 会覆盖值吗?
- diff - 在 Monaco Editor 中计算 3 路合并
- dart - 如何将 Dart 项目环境拆分为 prod、dev?
- three.js - 使用 alpha 遮罩其他对象的着色器
- amazon-web-services - Spring Cloud AWS 对 Spring Boot 2.4 的支持
- c++ - 高效的矩阵实现
- reactjs - 从useEffect控制台日志reactjs获取未定义的消息
- python - 客户端在循环中侦听和响应的服务器客户端不起作用(python)
- python - PySpark - 加载本地文件(图像)时路径不存在