我试图解析这个字符串:
[{"busses":[{"bus":1,"time":"2016-10-11 04:56:01","lat":"30.5198498","lon":"-90.47052981","acc":"6.0\n"},{"bus":2,"time":"2016-10-11 11:37:55","lat":"30.51764384","lon":"-90.47189947","acc":"3.0\n"},{"bus":3,"time":"2016-10-05 03:52:11","lat":"30.51982784","lon":"-90.47063428","acc":"8.0\n"},{"bus":4,"time":"2016-10-11 11:37:56","lat":"30.51998849","lon":"-90.47060976","acc":"3.0\n"}]}]
这是我的旧密码.
public class Bus
{
public string busNum { get; set; }
public string busTime { get; set; }
public string busLat { get; set; }
public string busLon { get; set; }
public string busAcc { get; set; }
}
public class Busses
{
public List<Bus> busses { get; set; }
}
public class TestCoordinate : MonoBehaviour
{
private float nextActionTime = 0.0f;
private float period = 5.0f;
void Start()
{
}
void Update()
{
if (Time.time > nextActionTime)
{
nextActionTime = Time.time + period;
StartCoroutine("GetCoordinatesUpdate");
}
}
IEnumerator GetCoordinatesUpdate()
{
WWW www = new WWW("Myurl.com");
yield return www;
string cleanurl = www.text.Replace("(", "").Replace(")", "");
string finalurl = "[{\"busses\":" + cleanurl + "}]";
Debug.Log(finalurl);
Busses busses = JsonMapper.ToObject<Busses>(finalurl);
}
}编辑:在引用链接: Serialize and Deserialize Json and Json Array in Unity之后,我已将代码更改为此,请参阅下面的错误:
public class Bus
{
public string bus;
public string Time;
public string Lat;
public string Lon;
public string Acc;
}
public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = UnityEngine.JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Items;
}
public static string ToJson<T>(T[] array)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return UnityEngine.JsonUtility.ToJson(wrapper);
}
[Serializable]
private class Wrapper<T>
{
public T[] Items;
}
}
public class TestCoordinate : MonoBehaviour
{
private float nextActionTime = 0.0f;
private float period = 5.0f;
public Bus[] busInstance;
public string lat;
void Start()
{
}
void Update()
{
if (Time.time > nextActionTime)
{
nextActionTime = Time.time + period;
StartCoroutine("GetCoordinatesUpdate");
}
}
IEnumerator GetCoordinatesUpdate()
{
//string url = "http://gdata.selu.edu/traxx/allpositions2.php";
WWW www = new WWW("http://gdata.selu.edu/traxx/allpositions2.php");
yield return www;
string cleanurl = www.text.Replace("(", "").Replace(")", "").Replace("\n","");
string finalurl = "{\"busses\":" + cleanurl + "}";
Debug.Log(finalurl);
busInstance = JsonHelper.FromJson<Bus>(finalurl);
lat = busInstance[0].Lat;
Debug.Log(lat);
}
}注意:但是,在试图显示lat时,我遇到了一个错误,即"NullReferenceException: Object未设置为对象的实例“。我也尝试了busInstance.Length,并收到了同样的错误。怎么了,我错过了一些我看不到的简单的东西吗?
我正试图使用来自JsonMapper.ToObject的LitJson将这4路总线放到列表中。
我引用了这篇文章:how to parse json array using Litjson?,...And,那篇文章是我建立这个文章的基础。但是,我收到了一个错误:“Busses类型不能充当数组”。
您将看到,我已经将"[{busses:“)连接到字符串中,以便使其类似于我引用的帖子。
备注:,我在使用LitJson和Unity。
发布于 2016-10-11 21:03:12
是的,你错过了一些简单的东西。在您所引用的帖子中,他们正在操作JSON以将其包围为{ "Items":和}。然后,JsonHelper.Wapper<T>类使用这个额外的JSON来提取JSON数组。包装类依赖于外部属性的名称为“Item”,但您已经将其更改为"busses“。因此,反序列化器无法填充保持为空的Item数组,然后最终将其分配给您的busInstance变量。因为busInstance是空的,所以当您尝试访问busInstance[0].Lat时,您将得到一个NullReferenceException。
因此,快速的解决办法似乎是改变这一行:
string finalurl = "{\"busses\":" + cleanurl + "}";对此:
string finalurl = "{\"Items\":" + cleanurl + "}";我不得不承认,我不喜欢这种处理JSON的方法,因为我觉得这真的很麻烦。显然,这样做的全部目的是绕过不支持顶级数组的团结序列化程序中的一个限制。在我看来,一种更好的方法是根本不尝试修改JSON (除了去掉外部括号(在本例中仍然是一个不幸的必要)),而是直接将JSON反序列化到总线数组中。您可以使用LitJSON轻松地做到这一点,您最初在“旧”代码中引用了该代码:
busInstance = JsonMapper.ToObject<Bus[]>(cleanurl);顺便提一下,我建议将busInstance变量重命名为busArray,因为它是一个数组,而不是一个Bus的实例。保持变量名的准确性对于其他人理解您的代码非常重要。
https://stackoverflow.com/questions/39982677
复制相似问题