"Unity" uses Jsonobject to parse JSON

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.