Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the ways to parse json in Unity

Shulou Source: shulou.com Published: 2022-06-02 09:42:47 10月04日 Update

This article is to share with you about the ways of parsing json in Unity. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Currently, there are several kinds of unity parsing json contacted:

1. The more popular listjson (data objects must be one-to-one with json)

two。 Currently, unity comes with JsonUtility (cannot parse complex json)

3.Newtonsoft.Json (it is recommended that complex json can be parsed without the need for one-to-one with json fields, so it is flexible)

(download address: https://github.com/SaladLab/Json.Net.Unity3D/releases)

Listjson provides a variety of json parsing, which can be parsed into JsonData or converted into custom mode data objects through JsonMapper.

ListJson is rich in features, but when converting to a custom object through JsonMapper, the fields of the custom object and the parsed json field must be one-to-one (the field in the json, the custom object must have), otherwise the parsing will fail.

For example:

Jison:

String json = @ "{" d "": "" d ","album": {"cc":"cc","name":"The Dark Side of the Moon","artist":"Pink Floyd" "" year "": 1973, "" tracks "": ["" Speak To Me "", "Breathe","On The Run"],"tt":"tt" "" tracksOb "": [{"" trackOb "": "11"}, {"trackOb":"22"}],"bb":"bb"}"

There are 3 mod corresponding to Json.

They are OO and TestModel,TestModel2

Public class OO {public string d; public TestModel album; public string bb;} public class TestModel {public string name; public string artist; public int year; public string [] tracks; public TestModel2 [] tracksOb;} public class TestModel2 {public string trackOb;}

The above is the json to be parsed and the corresponding custom model data

1. Parsing data using listJson

Analytical method public static T GetData2 (string text) where T: class {/ / JsonData table = AnalysisJson.Analy (text); T t = JsonMapper.ToObject (table.ToJson ()); / T t = JsonConvert.DeserializeObject (text); / / T t = JsonUtility.FromJson (text); return t;} OO o = GetData2 (json)

Running the parse will cause an error because the json string contains many objects, and fields

However, data OO and TestModel,TestModel2 objects do not all correspond to json string fields one by one, and listjson parsing is very rigorous.

two。 Use Newtonsoft.Json

Analytical method public static T GetData2 (string text) where T: class {/ / JsonData table = AnalysisJson.Analy (text); / / T t = JsonMapper.ToObject (table.ToJson ()); T t = JsonConvert.DeserializeObject (text); / / T t = JsonUtility.FromJson (text); return t;} OO o = GetData2 (json)

The operation will not report an error and will be fully parsed.

3. Use unity to bring your own JsonUtility

Analytical method public static T GetData2 (string text) where T: class {/ / JsonData table = AnalysisJson.Analy (text); / / T t = JsonMapper.ToObject (table.ToJson ()); / / T t = JsonConvert.DeserializeObject (text); T t = JsonUtility.FromJson (text); return t;} OO o = GetData2 (json)

These are the ways of parsing json in Unity, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Object field data one-to-one method way complex character string more knowledge article run rigorous practical functional can be passed address that is work will Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Apple NVidia Shulou Technology Microsoft