- Home /
Firebase Datasnapshot Json to Object using JsonUtility
I have a json from Firebase DataSnapShot using GetRawJsonValue. I have trouble converting it into object using JsonUtility.FromJson>(myjsonfromGetRawJsonValue). The sample json is as following:-
{"-LRpiJg_RP2re3UY30fG":{"orderslist":[{"Name":"Velvet Cake","Price":"10.9","Qty":1},{"Name":"green tea cake","Price":"8.9","Qty":1},{"Name":"latte","Price":"9.9","Qty":2},{"Name":"americano","Price":"5.9","Qty":2}],"rest_id":"dessert_house","table_number":"123","take_away":false,"user_id":"null"}, "-LRpjHchYhXTa0vY51JX":{"orderslist":[{"Name":"Velvet Cake","Price":10.899999618530273,"Qty":2},{"Name":"green tea cake","Price":8.8999996185302734,"Qty":1},{"Name":"latte","Price":9.8999996185302734,"Qty":2}],"rest_id":"dessert_house","table_number":"123","take_away":false,"user_id":"null"},"-LRplEOFsZ2j3A-aQYT4":{"orderslist":[{"Name":"Velvet Cake","Price":10.9,"Qty":1},{"Name":"latte","Price":9.9,"Qty":2},{"Name":"americano","Price":5.9,"Qty":1}],"rest_id":"dessert_house","table_number":"123","take_away":false,"user_id":"null"}
The serializable:-
[Serializable]
public class Orderslist
{
[SerializeField]
public string Name;
public double Price;
public int Qty;
}
[Serializable]
public class Orders
{
[SerializeField]
public string user_id;
public string rest_id;
public string date_time;
public bool take_away;
public string table_number;
public List<Orderslist> orderslist;
And how to access the children since the object child is System.String using Dictionary?
Just found out JsonUtility doesn't support Dictionary. Any solution either using back JsonUtility or other serializer?
Answer by Bunny83 · Dec 03, 2018 at 04:46 AM
Unity's JsonUtility is only meant to map actual classes and in many cases is not suitable for communicating with arbitary web apis.
You can use my SimpleJSON to parse arbitrary JSON data. Note that your sample data is missing a closing curly bracket at the end.
noted on the curly bracket, miss that one out when copy and paste. Had decided to turn all the key into object using splitting and appending before deserializing.
Your answer
Follow this Question
Related Questions
JsonUtility not found / working 0 Answers
Custom Editor: Weird reset when clicking Play 1 Answer
Stop specific fields from being serialized by JSON utility 3 Answers
How to protect JSON file game data? 3 Answers
Json Serialization documentation official Unity website - marking it [Serializable] caused error 1 Answer