> For the complete documentation index, see [llms.txt](https://xerial.gitbook.io/xerial-doc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xerial.gitbook.io/xerial-doc/gaming-toolkit/xerial-unity-sdk-guide/xerial-services/service-managers/inventory-manager/creating-nft-inventory.md).

# Creating NFT Inventory

## Usage

```csharp
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.

### &#x20;**NFT**&#x20;

First, we retrieve the node containing NFT data from the JSON response received from the server. c

<pre class="language-csharp"><code class="lang-csharp"><strong>NFT nft = new NFT();
</strong>
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&#x3C;Token>();

</code></pre>

### Collection

```csharp
nft.collection = new Collection();
nft.collection.Id = collectionNode["id"].Value;
nft.collection.TxHash = collectionNode["txHash"].Value;
```

### Metadata

```csharp
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

<pre class="language-csharp"><code class="lang-csharp"><strong>JSONNode contractnode = rootNode["metadata"]["contract"];
</strong>
nft.metadata.contract = new Contract();
nft.metadata.contract.address = contractnode["address"].Value;
</code></pre>

### Attributes

```csharp
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

<pre class="language-csharp"><code class="lang-csharp">nft.tokens = new List&#x3C;Token>();
foreach (JSONNode tokenNode in tokensArray)
{
    Token token = new Token();
    token.tokenId = tokenNode.AsInt;
<strong>    nft.tokens.Add(token);
</strong>}
</code></pre>

## 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.

<pre class="language-csharp"><code class="lang-csharp">if (XerialServices.instance.inventoryManager.inventory != null)
{
    XerialServices.instance.inventoryManager.inventory.AddNFT(nft);
}
else
{
    XerialServices.instance.inventoryManager.inventory = new NFTInventory();
<strong>    XerialServices.instance.inventoryManager.inventory.AddNFT(nft);
</strong>}
</code></pre>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://xerial.gitbook.io/xerial-doc/gaming-toolkit/xerial-unity-sdk-guide/xerial-services/service-managers/inventory-manager/creating-nft-inventory.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
