Why do you use Jsonobject
Since I've used jsonutility and Newton.json to parse json, why do we now use a new jsonobject to parse JSON?
- Using jsonutility:http://www.cnblogs.com/guxin/p/unity-jsonutility-parse-list-object.html
- Using newton.json:http://www.cnblogs.com/guxin/p/csharp-parse-json-by-newtonsoft-json-net.html
In Unity game development, when you use Newton.json to deserialize, you need to specify a certain type, what is the problem?
In the game's prop system, there is a parent class item class that contains the property ID and name, a subclass of consumable consuming category, containing the properties hp and MP,UML as follows:
The back end returns the item information JSON as follows:
[ { "ID":1, "name":"Blood Bottle", "type":"Consumable", "HP":Ten, "MP":0, }, { "ID":2, "name":"Blue Bottle", "type":"Consumable", "HP":0, "MP":Ten, }]
When using Newton.json, the code is as follows:
//Itemsjson is a JSON string containing the item information Public voidParseitemjson (stringItemsjson) {List<Item> itemList = newtonsoft.json.jsonconvert.deserializeobject<list<item>>(Itemsjson); foreach(Item TempinchitemList) { intID =temp.id; stringName =temp. Name; Item.itemtype type=temp. Type; Item Item=NULL; Switch(type) { CaseItem.ItemType.Consumable:Consumable Consumable= Temp asconsumable; intHP =Consumable. Gpt intMP =Consumable. MP; Item=Newconsumable (ID, name, type, HP, MP); Break; //other types omitted ... default: Break; }
Itemlist.add (temp); }}
According to the above idea, the item type to deserialize, and then according to Item.type to determine the specific sub-type of the item class, if for the consumable consumable type, get the type of HP and MP properties, and then by the consumable type to instantiate the object.
However, because the item type is specified in deserialization, the contents of the HP and MP are not resolved to the item object even if the JSON string contains the content.
So the problem is that when you resolve to a parent class, you want to transform to a subclass based on the attributes in the parent class, which can cause the transformation to fail!
Jsonobject How to use
You can now resolve the problem by using Jsonobject instead.
First download the Jsonobject in Assetstore and import it into the Unity project.
Based on its readme and its own demo, you can quickly learn to use the plugin. The code is modified to read as follows:
PrivateList<item> itemList =NewList<item>(); /// <summary> ///Parse Item JSON/// </summary> Public voidParseitemjson (stringItemsjson) {Jsonobject J=NewJsonobject (Itemsjson); foreach(Jsonobject Tempinchj.list) {intID = (int) temp["ID"].N; stringName = temp["name"].str; Item.itemtype type= (Item.itemtype) System.Enum.Parse (typeof(Item.itemtype), temp["type"].str); Item Item=NULL; Switch(type) { CaseItem.ItemType.Consumable:intHP = (int) temp["HP"].N; intMP = (int) temp["MP"].N; Item=Newconsumable (ID, name, type, HP, MP); Break; //other types omitted default: Break; }
Debug.Log ("item.id =" + Item.id + ", consumable.hp =" + ((consumable) item). HP); Itemlist.add (item); } }
After running, the JSON can be parsed correctly to get the property values of the parent class and the child class.
Learning materials:
- Http://www.sikiedu.com/course/40/task/545/show
"Unity" uses Jsonobject to parse JSON