2

apologies if I'm doing something wrong, this is my first post.

I'm currently working with C# and want to save a bunch of data out to a JSON file and load it back, but I'm having trouble figuring out how to get it in the following format.

// Primary ID
    001
    {
         // Secondary ID
         01
         {
              // Tertiary ID
              01
              {
                   string: "this is some information.",
                   int: 9371
              }
         }

         // Secondary ID
         02
         {
              // Tertiary ID
              01
              {
                   string: "blah blah blah.",
                   int: 2241
              }
         }
    }

I'd essentially like to be able to call up information with a particular set of IDs for example 001-02-01 which would return a string ("blah blah blah.") and an int (2241).

The reason I want to go about it like this instead of just having one longer ID is so that when the JSON file becomes very large, I'm hoping to be able to speed up the search for information by passing each ID in turn.

If that makes no sense and it would be equally as fast to just pass in one longer ID and not be bothered by this whole nested ID segments concept then please let me know!

If, however what I'm thinking is correct and it would help the speed of finding particular data by structuring it out like this, how would I go about doing that? With nested C# classes in arrays?

2 Answers 2

1

The most simple way and efficient way would be to have all data as same type. Currently, you seem to go for each object is of type of the given id:

{
   "01":{},
   "02" :{}
}

this will not go too well if trying to use a serializable class.

I would recommend the following:

{
    "items" : [
       {"id":"01" }, { "id":"02" },...
    ]
}

Then you can serialize/deserialize easily with

[Serializable]
public class Item
{
    public string id = null;
}
[Serializable]
public class RootObject
{
    public List<Item> items = null;
}

and then in Unity:

void Start(){
    string str = GetJson(); // However you get it
    RootObject ro = JsonUtility.FromJson<RootObject>(str);
}

if you want to speed up the fetching and your collection is large, convert to dictionary.

Dictionary<string, Item> dict = null;
void Start(){
    string str = GetJson(); // However you get it
    RootObject ro = JsonUtility.FromJson<RootObject>(str);
    this.dict = new Dictionary<string,Item>();
    foreach(Item item in ro.items){
        Item temp = temp;
        this.dict.Add(item.Id, temp);
    }
    ro = null;
}

Now you can access real fast.

Item GetItem(string id)
{
     if(string.IsNullOrEmpty(id) == true){ return null; }
     Item item = null;
     this.dict.TryGetValue(id, out item);
     return item;
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you end up storing millions of records in your file and want to start doing something more performant it would be easier to switch to a decent document database like MongoDB rather than trying to reinvent the wheel.

Worry about writing good standard code before worrying about performance problems that don't yet exist.

The following example is not in your language of choice but it does explain that JSON and arrays of 1,000,000 objects can be searched very quickly:

const getIncidentId = () => {
  let id = Math.random().toString(36).substr(2, 6).toUpperCase().replace("O", "0")
  return `${id.slice(0, 3)}-${id.slice(3)}`
}

console.log("Building array of 1,000,000 objects")
const littleData = Array.from({ length: 1000000 }, (v, k) => k + 1).map(x => ({ cells: { Number: x, Id: getIncidentId() } }))

console.log("Getting list of random Ids for array members [49, 60, 70000, 700000, 999999]")
const randomIds = ([49, 60, 70000, 700000, 999999]).map(i => littleData[i].cells.Id)
console.log(randomIds)
console.log("Finding each array item that contains a nested Id property in the randomIds list.")
const foundItems = littleData.filter(i => randomIds.includes(i.cells.Id))
console.log(foundItems)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.