Unity parsing JSON data

Time:2021-10-22

1. Use jsonutility to parse

Jsonutility is a JSON parsing tool that comes with unity.

1.1 create an object class that interprets data

It should be noted that each method needs to be serialized using [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 obtaining transfer data:

string jsonTest1 = File.ReadAllText(Application.dataPath + “/Resources/geojson_laneel_drive_paths_1603453072.json”, Encoding.UTF8);

1.3 analysis:

LaneelDrivePathsData obj1 = JsonUtility.FromJson<LaneelDrivePathsData>(jsonTest1);

1.4 printing results:

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 viewing print results:

It is not completely printed. After looking up various materials of the game, it is found that there is a problem when jsonutility parses [[], []] this format, and a “title” is required.

2. Use newtonsoft.json to interpret JSON data

Because the use of the built-in jsonutility can not meet my business needs, I continued to investigate and found that this is OK. Keep trying.

2.1 add newtonsoft.json

Open window – > asset store – > search JSON – > select json.net for unity

2.2 data acquisition

string jsonTest = File.ReadAllText(Application.dataPath + “/Resources/geojson_laneel_drive_paths_1603453072.json”, Encoding.UTF8);

2.3 writing data classes

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

}

You can use the automatic generation tool to add JSON data and generate data classes with one click. Address:

2.4 parsing data

LaneelDrivePathsDataTest obj = JsonConvert.DeserializeObject<LaneelDrivePathsDataTest>(jsonTest);

2.5 print parsed data

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