Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways to parse json in Unity

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report