unity解析json 数据

Posted 大不懂

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity解析json 数据相关的知识,希望对你有一定的参考价值。

1.使用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<List<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/convert/json2csharp

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);
                
            
            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);
        

2.6查看打印结果:

因为太多了无法展示,可自行打印。

附上json数据:

文件位置Assets下新建一个Resourses 将json文件添加进去。

"type": "FeatureCollection","features": ["type": "Feature","geometry": "type": "LineString","coordinates": [[116.284745906, 40.04171535, 37.648],[116.284748516, 40.041706397, 37.654],[116.284751117, 40.04169747, 37.659],[116.284753729, 40.041688504, 37.665],[116.284756377, 40.041679402, 37.67],[116.284759091, 40.041670067, 37.677],[116.28476186, 40.041660558, 37.68],[116.284764644, 40.041651043, 37.682],[116.284767446, 40.041641529, 37.685],[116.284770032, 40.041631915, 37.692],[116.284772611, 40.041622306, 37.699],[116.284775183, 40.041612703, 37.705],[116.284777749, 40.041603105, 37.712],[116.284780307, 40.041593513, 37.719]],"properties": "type": "DrivePath","parentLaneElId": 434015,"fromLaneElId": 434380,"toLaneElId": 433419, "type": "Feature","geometry": "type": "LineString","coordinates": [[116.290174105, 40.041253539, 37.539],[116.290176713, 40.041244827, 37.541],[116.290179408, 40.041235815, 37.544],[116.290182133, 40.041226696, 37.546],[116.290184916, 40.041217378, 37.549],[116.290187729, 40.041207947, 37.551],[116.290190507, 40.041198626, 37.554],[116.290193221, 40.041189511, 37.556],[116.290195866, 40.041180613, 37.559],[116.290198391, 40.041172114, 37.561],[116.290200773, 40.041164084, 37.564],[116.290203085, 40.041156279, 37.568],[116.290205371, 40.041148556, 37.571],[116.290207657, 40.041140828, 37.575],[116.290209994, 40.041132922, 37.578],[116.29021239, 40.041124812, 37.581],[116.2902148, 40.041116656, 37.585],[116.290217186, 40.041108577, 37.588],[116.290219571, 40.041100501, 37.591],[116.290221979, 40.041092347, 37.594],[116.290224373, 40.041084241, 37.598],[116.290226708, 40.041076339, 37.601],[116.290228992, 40.041068614, 37.604],[116.290231274, 40.041060896, 37.607],[116.29023358, 40.041053106, 37.611],[116.29023595, 40.041045106, 37.613],[116.29023846, 40.041036642, 37.616],[116.290241092, 40.041027779, 37.619],[116.290243687, 40.041019348, 37.622],[116.29024635, 40.041011134, 37.625],[116.29024908, 40.041003135, 37.628],[116.290251878, 40.040995353, 37.631],[116.290254744, 40.040987786, 37.634],[116.290257677, 40.040980436, 37.637]],"properties": "type": "DrivePath","parentLaneElId": 434007,"fromLaneElId": 433979,"toLaneElId": 433369]

 

以上是关于unity解析json 数据的主要内容,如果未能解决你的问题,请参考以下文章

unity解析json 数据

Unity 解析Json格式

在 c# (Unity) 中解析 JSON 文件

使用 SimpleJSON 从 Web API 解析 Unity 中的 JSON 数据数组

如何从 JSON 解析到 Unity GameObject

Unity3d数据存储 PlayerPrefs,XML,Json数据的存储与解析