Problem Parsing Json (JsonUtility)

Hello, this seems like a simple problem to solve but I’ve been stuck on it for a while now. I’ve successfully parsed data from the google API, however, I’m having issues when calling json data from a text file sitting on my webserver.

Below is the Json data on the webserver:

{
  "Menu": {
    "status": "OK",
    "Restaurants": [
      {
        "Restaurant": {
          "name": "McDonalds",
          "MenuItems": [
            {
              "category": "PrimaryItem",
              "name": "Classic Double Bacon QPC",
              "price": 6.89,
              "calories": 870
            },
            {
              "category": "PrimaryItem",
              "name": "Classic Bacon QPC",
              "price": 5.89,
              "calories": 670
            },
            {
              "category": "Sides",
              "name": "Icecream Cone",
              "price": 0.89,
              "calories": 70
            }
          ]
        }
      },
      {
        "Restaurant": {
          "name": "BurgerKing",
          "MenuItems": [
            {
              "category": "PrimaryItem",
              "name": "Whooper",
              "price": 3.89,
              "calories": 470
            },
            {
              "category": "Sides",
              "name": "Milkshake",
              "price": 0.89,
              "calories": 70
            }
          ]
        }
      }
    ]
  }
}

This is my MenuObject:

using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class MenuItem
{
    public string category;
    public string name;
    public double price;
    public int calories;
}
[System.Serializable]
public class Restaurant
{
    public string name;
    public List<MenuItem> MenuItems;
}
[System.Serializable]
public class Menu
{
    public string status;
    public List<Restaurant> Restaurants;
}
[System.Serializable]
public class MenuObject
{
    public Menu Menu;
}

And this is the Unity code that parses the data:

 IEnumerator RequestRestaurantMenus()
    {
        WWW www = new WWW(url);
        yield return www;

        if (www.error == null)
        {
            Debug.Log(www.text); // dumps json data here!
            string jsondata = www.text;
            MenuObject menuObject = JsonUtility.FromJson<MenuObject>(jsondata);

            if (menuObject.Menu.status == "OK")
            {
                Debug.Log("Name: " + menuObject.Menu.Restaurants[0].name);

                foreach (Restaurant r in menuObject.Menu.Restaurants)
                {
                    Debug.Log(r.name);
                }
                //GenerateMenuList(menuObject);
            }
            else
            {
                Debug.Log("Invalid Parameters");
            }
        }
        else
        {
            Debug.Log("ERROR: " + www.error);
        }
    }

When I debug in unity it shows that it has stored two restaurants, however, when I access the restaurant info it’s shows an empty string on the first debug call above, and null when called from the foreach loop. The call for Menu.status is actually successful.

I’m guessing my MenuObject is incorrect, or I have a problem with my json structure. Anyone have any advice for me?

Ok, the problem is that the Restaurants are a list/array of objects containing Restaurant objects. So you need to add another class:

using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class MenuItem
{
	public string category;
	public string name;
	public double price;
	public int calories;
}
[System.Serializable]
public class Restaurant
{
	public string name;
	public List<MenuItem> MenuItems;
}
[System.Serializable]
public class Restaurants
{
	public Restaurant Restaurant;
}
[System.Serializable]
public class Menu
{
	public string status;
	public List<Restaurants> Restaurants;
}
[System.Serializable]
public class MenuObject
{
	public Menu Menu;
}

I was able to use Debug.Log(menu.Menu.Restaurants[0].Restaurant.MenuItems[0].name); successfully with this.