- Home /
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?
Answer by Ymrasu · Mar 07, 2019 at 07:46 PM
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.
Thanks a lot! I guess I need to study up more on json structures. You're a huge help.
Note that Restaurants is a bad name for that extra class. It represents one restaurant, so the plural is confusing. You might want to call one Restaurant and the other RestaurantItem / RestaurantData.
Though the best solution would be to get rid of that additional class in the json data. Of course this may not be an option if the json data isn't provided by a service under your control.
Note that Unity's JsonUtility is quite limited. It can only work with field names that can be represented by actual class fields (has to start with a letter, no spaces, slashes or dashes). If you're looking for a general purpose json parser, I've written SimpleJSON for this purpose. It does not deserialize the data into specialized classes, but represents the data with a node graph. This works for any kind of json data. I've also created some extensions for easy handling of common unity types (like vectors, quaternions, ...)
Your answer
Follow this Question
Related Questions
WWW data acquisition from remote server failing 2 Answers
Integrating Watson Speech-to-Text with Unity 1 Answer
Multiple Cars not working 1 Answer
Retreive Jsonfile and Read It 0 Answers
Distribute terrain in zones 3 Answers