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

{

/// 

/// 

/// 

public string type;

/// 

/// 

/// 

public List features;

}

[System.Serializable]

public class Geometry1

{

/// 

/// 

/// 

public string type;

/// 

/// 

/// 

public List> coordinates;

}

[System.Serializable]

public class Properties1

{

/// 

/// 

/// 

public string type;

/// 

/// 

/// 

public int parentLaneElId;

/// 

/// 

/// 

public int fromLaneElId;

/// 

/// 

/// 

public int toLaneElId;

}

[System.Serializable]

public class FeaturesItem1

{

/// 

/// 

/// 

public string type;

/// 

/// 

/// 

public Geometry1 geometry;

/// 

/// 

/// 

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(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

{

/// 

/// 

/// 

public string type { get; set; }

/// 

/// 

/// 

public List features { get; set; }

}

public class Geometry

{

/// 

/// 

/// 

public string type { get; set; }

/// 

/// 

/// 

public List> coordinates { get; set; }

}

public class Properties

{

/// 

/// 

/// 

public string type { get; set; }

/// 

/// 

/// 

public int parentLaneElId { get; set; }

/// 

/// 

/// 

public int fromLaneElId { get; set; }

/// 

/// 

/// 

public int toLaneElId { get; set; }

}

public class FeaturesItem

{

/// 

/// 

/// 

public string type { get; set; }

/// 

/// 

/// 

public Geometry geometry { get; set; }

/// 

/// 

/// 

public Properties properties { get; set; }

}

可以使用自动生成工具,将json数据添加进去一键生成数据类。地址:

2.4解析数据

LaneelDrivePathsDataTest obj = JsonConvert.DeserializeObject(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);

你可能感兴趣的:(unity解析json 数据)