.使用JsonUtility解析
JsonUtility是unity自帶的解析json的工具。
1.1具體使用創建一個解釋數據的對象類
需要注意的是需要將每一個方法序列化,使用[System.Serializable]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class LaneelDrivePathsData
{
/// <summary> /// /// </summary> public string type; /// <summary> /// /// </summary> public List<FeaturesItem1> features;
}
[System.Serializable]
public class Geometry1
{
/// <summary> /// /// </summary> public string type; /// <summary> /// /// </summary> public List<Listwww.cungun.com<double>> coordinates;
}
[System.Serializable]
public class Properties1
{
/// <summary> /// /// </summary> public string type; /// <summary> /// /// </summary> public int parentLaneElId; /// <summary> /// /// </summary> public int fromLaneElId; /// <summary> /// /// </summary> public int toLaneElId;
}
[System.Serializable]
public class FeaturesItem1
{
/// <summary> /// /// </summary> public string type; /// <summary> /// /// </summary> public Geometry1 geometry; /// <summary> /// /// </summary> public Properties1 properties;
}
1.2獲取接送數據:
string jsonTest1 = File.ReadAllText(Application.dataPath + "/Resources/geojson_laneel_drive_paths_1603453072.json", Encoding.UTF8);
1.3解析:
LaneelDrivePathsData obj1 = JsonUtility.FromJson<LaneelDrivePathsData>(jsonTest1);
1.4打印結果:
foreach (var inter in obj1.features)
{
Debug.Log("********************************************"); Debug.Log("features.type:" + inter.type); Debug.Log("======================================="); Debug.Log("features.properties.type:" + inter.properties.type); Debug.Log("features.properties.parentLaneElId:" + inter.properties.parentLaneElId); Debug.Log("features.properties.fromLaneElId:" + inter.properties.fromLaneElId); Debug.Log("features.properties.toLaneElId:" + inter.properties.toLaneElId); Debug.Log("======================================="); Debug.Log("features.geometry.type:" + inter.geometry.type); foreach (var coor in inter.geometry.coordinates) { Debug.Log("____________________________________"); foreach (var co in coor) { Debug.Log("features.geometry.coordinates:" + co); } } }
1.5查看打印結果:
並沒有完全打印,經過查找看游戲的各種資料發現JsonUtility再解析[ [],[] ]這種格式的時候是有問題的,需要有一個“title”才可以。
2.使用Newtonsoft.Json解釋json 數據
因為使用自帶的JsonUtility是不能滿足我業務需求的,於是繼續調研發現這個是可以的。繼續嘗試。
2.1添加Newtonsoft.Json
打開Window->Asset Store->搜索json->選擇JSON.NET For Unity
2.2獲取數據
string jsonTest = File.ReadAllText(Application.dataPath + "/Resources/geojson_laneel_drive_paths_1603453072.json", Encoding.UTF8);
2.3編寫數據類
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LaneelDrivePathsDataTest
{
/// <summary> /// /// </summary> public string type { get; set; } /// <summary> /// /// </summary> public List<FeaturesItem> features { get; set; }
}
public class Geometry
{
/// <summary> /// /// </summary> public string type { get; set; } /// <summary> /// /// </summary> public List<List<double>> coordinates { get; set; }
}
public class Properties
{
/// <summary> /// /// </summary> public string type { get; set; } /// <summary> /// /// </summary> public int parentLaneElId { get; set; } /// <summary> /// /// </summary> public int fromLaneElId { get; set; } /// <summary> /// /// </summary> public int toLaneElId { get; set; }
}
public class FeaturesItem
{
/// <summary> /// /// </summary> public string type { get; set; } /// <summary> /// /// </summary> public Geometry geometry { get; set; } /// <summary> /// /// </summary> public Properties properties { get; set; }
}
可以使用自動生成工具,將json數據添加進去一鍵生成數據類。地址:
https://www.bejson.com/conver...
2.4解析數據
LaneelDrivePathsDataTest obj = JsonConvert.DeserializeObject<LaneelDrivePathsDataTest>(jsonTest);
2.5打印解析的數據
foreach (var inter in obj.features)
{
Debug.Log("********************************************"); Debug.Log("features.type:" + inter.type); Debug.Log("======================================="); Debug.Log("features.geometry.type:" + inter.geometry.type); foreach (var coor in inter.geometry.coordinates) { Debug.Log("____________________________________" ); foreach (var co in coor) { Debug.Log("features.geometry.coordinates:" + co); } }