J
J
JoJikRe2021-08-27 12:11:54
JSON
JoJikRe, 2021-08-27 12:11:54

How to read a lot of Json data?

I am storing a lot of data in json.

json file looks like this:
{"arrow":-5.0,"pos":2.0,"speed":1,"section":0,"empty":""}{"arrow":-4.0,"pos": 1.0,"speed":1,"section":0,"empty":""}{"arrow":-3.0,"pos":1.0,"speed":1,"section":0,"empty" :""}{"arrow":-2.0,"pos":0.0,"speed":1,"section":0,"empty":""}

If you read the file using ReadAllText, an error occurs. If I read it in other ways, then it simply displays what is
stored in the file.

I need to put all these values ​​into an array.

My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Net;
 
public class SaveToJson : MonoBehaviour
{
    public SaveData data = new SaveData();
 
    private SaveData _data = new SaveData();
 
    private string path;
 
    public int ac, pc, spc, sc;
 
    void Awake()
    {
        path = Application.streamingAssetsPath + "/file.json";
    }
 
    private void Start()
    {
 
 
        
    }
 
    public void Save(float arrow, float pos, int speed, int section)
    {
        ac++;
        pc++;
        spc++;
        sc++;
 
        data.arrow = arrow;
 
        data.pos = pos;
 
        data.speed = speed;
 
        data.section = section;
 
        File.AppendAllText(path, JsonUtility.ToJson(data));
    }
 
    public float GetArrow()
    {
        _data = JsonUtility.FromJson<SaveData>(File.ReadAllText(path));
 
        return _data.arrow;
    }
 
    public float GetPos()
    {
        _data = JsonUtility.FromJson<SaveData>(File.ReadAllText(path));
 
        return _data.pos;
    }
 
    public void Delete()
    {
        File.WriteAllText(path, data.empty);
    }
 
    [System.Serializable]
    public class SaveData
    {
        public float arrow;
 
        public float pos;
 
        public int speed;
 
        public int section;
 
        public string empty;
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2021-08-27
@tentrun

Have you tried looping?

G
Griboks, 2021-08-27
@Griboks

Save data not to a file, but to a variable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question