Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

c# - What's the best way to loop through a JSON of sales data to create a graph in Unity?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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}"); }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...