数据格式
今天使用Unity解析地理信息(Gis),.geojson格式文件,并根据解析数据中的经纬度在地球模型上加载相应效果;
总结几种不同Json解析方法
目前接触的unity 解析json 有几种:
1.目前unity自带 JsonUtility(JsonUtility要加上序列化标标签,才可以解析)
2. 比较流行的listjson(数据对象必须和json一对一) 解析不了Float类型数据!!!
3.Newtonsoft.Json (可以解析复杂的json,不需要和json字段一对一 ,灵活,没怎么用过,听说很牛逼,有兴趣的同学可以调研一下)
以.geojson格式文件,分别使用上述方法测试效果:.geojson其实和json差不多上,只是用这种格式来定义地理信息中的json,就像是在众多程序员中用秃头来定义大神。。。。。。,geojson格式如下,由于很长,这里只把结构列出来:
{
"type": "FeatureCollection",
"layerName": "",
"maxRecord": "10",
"groupJSON": "",
"features": [
{
"type": "Feature",
"properties": {
"sort": "1",
"date": "2019-5-30",
"time": "0:00:41",
"lon": "111.9157",
"lat": "18.46912",
"shape": "111.9157,18.46912",
"strenth": "-56.2831",
"gradient": "-28.81694",
"error": "76.67672",
"loc_mode": "6",
"province": "",
"city": "",
"area": ""
},
"geometry": {
"type": "Point",
"coordinates": [
18.469120025634766,
111.91570281982422
]
}
},还有很多.......
]
}
文件放在了Assets下
口诀:(感谢我A哥教我口诀。)
1、看见“”{}“”定义类
2、 看见“【】”定义相应类型数组
3、 看见键值定义相应类型字段
所以上面的个格式就是
using UnityEngine;
using System.IO;
class GeoJson_Data
{
public string type;
public string layerName;
public int maxRecord;
public string groupJSON;
public features[] features;
}
public class features
{
public string type;
public properties properties;
public geometry geometry;
}
public class properties
{
public string sort;
public string date;
public string time;
public string lon;
public string lat;
public string shape;
public string strenth;
public string gradient;
public string error;
public string loc_mode;
public string province;
public string city;
public string area;
}
public class geometry
{
public string type;
public double[] coordinates;
}
public class Test_ReadJson : MonoBehaviour {
// Use this for initialization
void Start() {
string text = File.ReadAllText(Application.dataPath + "/data.geojson");
print(text);
GeoJson_Data geoJson_Data = JsonUtility.FromJson(text);
Debug.Log(geoJson_Data.type);
Debug.Log(geoJson_Data.layerName);
Debug.Log(geoJson_Data.maxRecord);
Debug.Log(geoJson_Data.groupJSON);
print(geoJson_Data.features.Length);
}
// Update is called once per frame
void Update () {
}
}
当时我反复校验我是不是哪里写错了,最后发现我的json文件前面的字段可以读取出来,到那个features数组里死活读取不出来,这时Unity自带的JsonUlitity弊端就显示出来了,无法解析复杂json结构。
前期准备:
下载Litjson.dll文件放到,Unity中Plugins文件下(为甚放这个文件夹,自己百度一下Unity特殊文件的用处),这里提供一个Litjson下载地址:点击这里
using UnityEngine;
using System.IO;
//一定要引入Litjson
using LitJson;
public class Test_ReadJson : MonoBehaviour {
// Use this for initialization
void Start() {
//将文件转化为txt格式
string txt = File.ReadAllText(Application.dataPath + "/data.geojson");
JsonData geojson_data=JsonMapper.ToObject(txt);
Debug.Log(geojson_data["type"].ValueAsString());
Debug.Log(geojson_data["layerName"].ValueAsString());
Debug.Log(geojson_data["maxRecord"].ValueAsInt());
JsonData features = geojson_data["features"];
foreach(JsonData feature in features)
{
Debug.Log(feature["type"].ValueAsString());
Debug.Log("-------------------------properties-----------------------");
Debug.Log(feature["properties"]["sort"].ValueAsString());
Debug.Log(feature["properties"]["date"].ValueAsString());
Debug.Log(feature["properties"]["time"].ValueAsString());
Debug.Log(feature["properties"]["lon"].ValueAsString());
Debug.Log(feature["properties"]["lat"].ValueAsString());
Debug.Log(feature["properties"]["shape"].ValueAsString());
Debug.Log(feature["properties"]["strenth"].ValueAsString());
Debug.Log(feature["properties"]["gradient"].ValueAsString());
Debug.Log(feature["properties"]["error"].ValueAsString());
Debug.Log(feature["properties"]["loc_mode"].ValueAsString());
Debug.Log(feature["properties"]["province"].ValueAsString());
Debug.Log(feature["properties"]["city"].ValueAsString());
Debug.Log(feature["properties"]["area"].ValueAsString());
Debug.Log("-------------------------geometry----------------------");
Debug.Log(feature["geometry"]["type"].ValueAsString());
Debug.LogFormat("({0},{1})", feature["geometry"]["coordinates"][0].ValueAsDouble(), feature["geometry"]["coordinates"][1].ValueAsDouble());
}
}
}
运行正常,全部输出;这里可以看出Litjson十分灵活,不需要定义类等一系列字段,按键值取相应数据就可以了。
最后,如果这篇文章对你有所帮助,鼓励一下再走吧,谢谢支持。