How can I turn JSON String into a list of objects based on number of items.

Hey there, I am receiving a JSON string from the backend of my webserver. I managed to create a class for User profile and mapped it with the exact fields as its JSON string. Unfortunately the Items JSON string has nested values. How can I create an object based on the number of items in my JSON string?

When I parsed the User JSON string to a class I used this code (the class had strings for each of the values - The Items JSON string has multiple items):

string  m_Json_USERInfo = userJSONString;
UserProfile m_LoadUserData= JsonUtility.FromJson<UserProfile>(m_Json_USERinfo);

I cannot reuse the same class structure. How do I store the Items in some sort of array?

This is my Items JSON string reformatted using Notepad ++

[{
    "name": "Mobile",
    "weight": "100",
    "price": "120",
    "cost": "5",
    "length": "100",
    "stock": 100,
    "sku": "24425",
    "height": "100",
    "owner": "grandwiz"
},
{
    "name": "Charger",
    "weight": "150",
    "price": "10",
    "cost": "4",
    "length": "100",
    "stock": 50,
    "sku": "35288",
    "height": "60",
    "owner": "grandwiz"
},
{
    "stock": 100,
    "sku": "43827",
    "height": "30",
    "associatedUser": "8obUGh7PXrOvBgpBZfvfzyss7uI3",
    "description": "Fresh and juicy",
    "width": "30",
    "name": "Red Apple",
    "owner": "grandwiz"
}]

The build in “tiny JSON” in Unity doesn’t handle stuff like this (free form Dictionary items). It only handles hard-shaped data.

If you go to the asset store you can get the JSON .NET package and that can probably deserialize what you have.

You may need to shape it slightly differently.

Does this work?UserProfile[] m_LoadUserData= JsonUtility.FromJson<UserProfile[]>(m_Json_USERinfo);

I already have JSON.NET Imported, but even if de-serialized, how do I get it in a manageable format? For each “owner” create new list or array?

It is your data, so you would need to decide what makes it “manageable” and then convert it appropriately.

Once you determine the precise steps to do this, you can begin to develop a strategy for managing it.

Otherwise you can just leave it in a Dictionary.

You can also use a web service like this to produce C# code for fixed-shape JSON blob, or parts thereof:

https://quicktype.io/csharp/

I have changed it to this:

UserInventory[] m_LoadInventories = JsonUtility.FromJson<UserInventory[]>(m_Json_InvInfo);

Although the user inventory class is empty as I dont really know how to structure it compared to UserProfile class. There were no nested values in UserProfile so I could just make public strings with the same value name in the JSON.
Here is my UserProfile class:

[System.Serializable]
public class UserProfile
{

    public string displayName;
    public string firstName;
    public string email;
    public string age;
    public string haircolor;
}

It was straight forward as there were not two values with the same name (nothing nested in the JSON). But the Items JSON has strings with the same name; name, stock, sku, etc.

I was not successful in storing the user data as a dictionary, so i resorted in creating a class to parse the JSON values. Could you perhaps point me to some good documentation or tutorials on Dictionaries. I just couldnt wrap my head around them when I was trying the parse the JSON values.

I could do no better than google really.

I have cleaned up my code into a switch statement. The USERINFO switch statement works, as per the UserProfile class I posted above. But how would i structure my UserInventory class in a similar fashion to UserProfile?

            switch (_Type)
            {
                case USERINFO:
                    m_Json_UserInfo = www.downloadHandler.text;

                    m_LoadData = JsonUtility.FromJson<UserProfile>(m_Json_UserInfo);
                    this.GetComponent<User>().SetUser(m_LoadData);
                    Debug.Log("User Json:   " + m_Json_UserInfo.ToString());
                    m_LoadData = null;

                    Statemachine.SetCurrentState(2);
                    break;
                case USERINVENTORY:
                    m_Json_InvInfo = www.downloadHandler.text;

                    m_LoadInventories = JsonUtility.FromJson<UserInventory[]>(m_Json_InvInfo);
                    //    this.GetComponent<User>().SetInventory(m_LoadInventories);
                    Debug.Log("Inventory Json:   " + m_Json_InvInfo.ToString());
                    //     m_LoadInventories = null;
                    break;
                case USERORDERS:
                    break;
            }

Solved.
Solution: being gangsta.

jk,
Within the class for user inventories i created another class to act as the nested fields. Works like a charm

“Ain’t it good to be a gangsta?”

Glad it worked out for you… carry on!

For anyone still facing this issue, i created a simple library that handle it and much more.
Link:
https://github.com/zakarialouqdyeme/ZVJson-Library