2

I used this tutorial to help me make graphs in Unity, however I want the data points in it to be created based on a JSON that contains sales data. How would you guys advise I do that? I am still learning, so any help would be extremely helpful.

Fattie
  • 27,874
  • 70
  • 431
  • 719
Sam Curry
  • 445
  • 2
  • 11
  • Looking forward to this 3D sales data app! – Brian Risk Jul 27 '16 at 15:55
  • Please do not vandalise your post. This may result in a [question ban](http://stackoverflow.com/help/question-bans). By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](https://meta.stackoverflow.com/q/323395) –  Jan 26 '19 at 01:41

1 Answers1

2

These days

JSON is Built Into Unity.

https://docs.unity3d.com/Manual/JSONSerialization.html

It's that easy.

A HUGE cause of confusion with Unity is that you get a lot of really old example code on the web.

  • Ten+ years ago you could use languages other than C# with Unity. Nowadays it is only C#. But you still get 1000s of questions asking why javascript, etc, doesn't work!

  • Ten+ years ago Unity had a crappy "UI" system. It now has a superb UI system (it's called ".UI"). But you still get many questions about the ridiculous early-days UI system.

  • In ancient Unity, you had to use pooling in even the simplest cases, say for bullets. Unity nowadays vastly improved performance and pooling is totally unnecessary in normal game situations.

  • For some reason Unity named the simple timer "Invoke" rather than "Timer" - this has led to 1000s of questions!

Be careful of incredibly out-of-date references to HandyJSON, SuperJSON, MegaJSON, JSONFinallyWithLessBugs, WhoaICanUseJSON and other rubbish packages.


Here is a simple example of parsing some Json from a text file, and in this case putting it into a Dictionary:

using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;

public class JsonTexts : MonoBehaviour
{
    public TextAsset ta; // drag to link in Editor
    [NonSerialized] public Dictionary<string, JsonParsePerson> persons;

    [Serializable]
    public class JsonParsePerson
    {
        public string id;
        public string firstname;
        public string lastname;
    }

    [Serializable]
    public class JsonParsePersons
    {
        public JsonParsePerson[] persons;
    }

    void Start()
    {
        JsonParsePersons pp = JsonUtility.FromJson<JsonParsePersons>(ta.text);
        persons = pp.persons.ToDictionary(i => i.id, i => i);

        // foreach (JsonParsePerson p in pp.persons)
        //   { Debug.Log($"it worked {p.id} {p.firstname}"); }
    }
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Fattie
  • 27,874
  • 70
  • 431
  • 719