Creating NFT Inventory After requesting the project inventory from Xerial services, the JSON response needs to be processed to create an instance of the UserData class.
Usage
Copy using System . Collections . Generic ;
using UnityEngine ;
using SimpleJSON ;
using Xerial ;
public class NFTResponseHandler
{
public void HandleNFTResponse ( string response)
{
XerialServices . instance . inventoryManager . inventory = null ;
JSONArray jsonArray = JSON . Parse (response). AsArray ;
if (jsonArray != null && jsonArray . Count > 0 )
{
JSONNode rootNode = jsonArray [ 0 ];
//Deserialize the object array
}
}
Deserializing NFTs from Project Inventory Data
To deserialize the NFTs from the JSON response within the project inventory data.
NFT
First, we retrieve the node containing NFT data from the JSON response received from the server. c
Copy NFT nft = new NFT ();
int quantity = rootNode [ "quantity" ]. AsInt ;
nft . amount = quantity;
JSONNode collectionNode = rootNode [ "collection" ];
nft . collection = new Collection ();
JSONNode metadataNode = rootNode [ "metadata" ];
nft . metadata = new Metadata ();
JSONArray tokensArray = rootNode [ "tokenIds" ]. AsArray ;
nft . tokens = new List < Token >();
Collection
Copy nft . collection = new Collection ();
nft . collection . Id = collectionNode [ "id" ]. Value ;
nft . collection . TxHash = collectionNode [ "txHash" ]. Value ;
Metadata
Copy nft . metadata = new Metadata ();
nft . metadata . name = metadataNode [ "name" ]. Value ;
nft . metadata . description = metadataNode [ "description" ]. Value ;
nft . metadata . image = metadataNode [ "image" ]. Value ;
nft . metadata . externalUrl = metadataNode [ "externalUrl" ]. Value ;
JSONNode contractnode = rootNode [ "metadata" ][ "contract" ];
nft . metadata . contract = new Contract ();
nft . metadata . attributes = new List < Attributes >();
foreach ( JSONNode attributeNode in metadataNode [ "attributes" ]. AsArray )
{
Attributes attribute = new Attributes ();
nft . metadata . attributes . Add (attribute);
}
Contract
Copy JSONNode contractnode = rootNode [ "metadata" ][ "contract" ];
nft . metadata . contract = new Contract ();
nft . metadata . contract . address = contractnode [ "address" ]. Value ;
Attributes
Copy nft . metadata . attributes = new List < Attributes >();
foreach ( JSONNode attributeNode in metadataNode [ "attributes" ]. AsArray )
{
Attributes attribute = new Attributes ();
attribute . type = attributeNode [ "type" ]. Value ;
attribute . name = attributeNode [ "name" ]. Value ;
attribute . value = attributeNode [ "value" ]. Value ;
nft . metadata . attributes . Add (attribute);
}
Token
Copy nft . tokens = new List < Token >();
foreach ( JSONNode tokenNode in tokensArray)
{
Token token = new Token ();
token . tokenId = tokenNode . AsInt ;
nft . tokens . Add (token);
}
Store the NFT inventory
Create NFT Instances : For each NFT entry in the JSON data, we add an instance of the NFT class to represent the NFT inside the inventory.
Copy if ( XerialServices . instance . inventoryManager . inventory != null )
{
XerialServices . instance . inventoryManager . inventory . AddNFT (nft);
}
else
{
XerialServices . instance . inventoryManager . inventory = new NFTInventory ();
XerialServices . instance . inventoryManager . inventory . AddNFT (nft);
}
Last updated 4 months ago