unity Several commonly used analytical way json

Reprinted from: https://blog.csdn.net/tianyongheng/article/details/78989612

Currently unity parsing json contact There are several:
1. The more popular listjson (data and json object must be one)
2. The current unity comes JsonUtility (can not resolve the complex json)
3.Newtonsoft.Json (recommendation can resolve complex the json, and json field does not need one, flexible)
(Download: HTTPS: //github.com/SaladLab/Json.Net.Unity3D/releases)

listjson to parse json provide diversification can be resolved into jsonData, also It can be converted into mode defined by the data object from JsonMapper.
listJson feature rich, but when converted to a custom object by JsonMapper, custom objects and fields to one field json parsing (json inside the field, a custom object must have), otherwise it will fail to resolve .
For example:
jison:

 1 string json = @"
 2           {
 3              ""d"":""d"",
 4             ""album"" : {    
 5              ""cc"":""cc"",          
 6               ""name""   : ""The Dark Side of the Moon"",
 7               ""artist"" : ""Pink Floyd"",
 8               ""year""   : 1973,         
 9               ""tracks"" : [
10                 ""Speak To Me"",
11                 ""Breathe"",
12                 ""On The Run""
13               ],
14               ""tt"":""tt"",
15              ""tracksOb"":[{""trackOb"":""11""},{""trackOb"":""22""}],
16              ""bb"":""bb""         
17             }
18           }
19         ";

Corresponds to the mod Json has 3
are OO, TestModel, TestModel2

 1 public class OO
 2 {
 3     public string d;
 4     public TestModel album;
 5     public string bb;
 6 }
 7 
 8 public class TestModel
 9 {
10     public string name;
11     public string artist;
12     public int year;
13     public string[] tracks;
14     public TestModel2[] tracksOb;
15 }
16  
17  public  class TestModel2
 18  {
 19,      public  string trackOb;
20  }

These are to be parsed and the corresponding custom json data model
1. Analytical data listJson

 1 解析方法
 2  public static T GetData2<T>(string text) where T : class
 3     {
 4         //JsonData table = AnalysisJson.Analy<JsonData>(text);
 5          T t = JsonMapper.ToObject<T>(table.ToJson());
 6 
 7          //T t = JsonConvert.DeserializeObject<T>(text);
 8         //T t = JsonUtility.FromJson<T>(text);
 9         return t;
10     }
1 OO o = GetData2<OO>(json);

Run the resolution that is being given, because json string contains many objects, and fields
and data OO, TestModel, TestModel2 object, and not all json string fields and one to one, listjson resolved very strict.

2. Use Newtonsoft.Json

 1 解析方法
 2  public static T GetData2<T>(string text) where T : class
 3     {
 4         //JsonData table = AnalysisJson.Analy<JsonData>(text);
 5          //T t = JsonMapper.ToObject<T>(table.ToJson());
 6 
 7          T t = JsonConvert.DeserializeObject<T>(text);
 8         //T t = JsonUtility.FromJson<T>(text);
 9         return t;
10     }
1 OO o = GetData2<OO>(json);

Run without error, completely resolved.

3. Use the unity that comes JsonUtility

 1 解析方法
 2  public static T GetData2<T>(string text) where T : class
 3     {
 4         //JsonData table = AnalysisJson.Analy<JsonData>(text);
 5          //T t = JsonMapper.ToObject<T>(table.ToJson());
 6 
 7         // T t = JsonConvert.DeserializeObject<T>(text);
 8         T t = JsonUtility.FromJson<T>(text);
 9         return t;
10     }
1 OO o = GetData2<OO>(json);

run. Some objects not resolved, will complain

Guess you like

Origin www.cnblogs.com/leonpf/p/11762867.html