【问题标题】:Multiple Nested JSON information - C# Process多个嵌套 JSON 信息 - C# 进程
【发布时间】:2019-01-30 11:30:46
【问题描述】:

抱歉,如果我做错了什么,这是我的第一篇文章。

我目前正在使用 C# 并希望将一堆数据保存到 JSON 文件中并将其加载回来,但我无法弄清楚如何以下列格式获取它。

// 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
              }
         }
    }

我基本上希望能够使用一组特定的 ID 调用信息,例如 001-02-01,它将返回一个字符串 (“blah blah blah.”) 和一个 int (2241)。

我想这样做而不是只使用一个更长的 ID 的原因是,当 JSON 文件变得非常大时,我希望能够通过传递每个 ID 来加快信息搜索速度转。

如果这没有意义,并且只传递一个更长的 ID 并且不受整个嵌套 ID 段概念的困扰,那么请告诉我!

但是,如果我的想法是正确的,并且通过这样的结构有助于加快查找特定数据的速度,我将如何去做?使用数组中的嵌套 C# 类?

【问题讨论】:

    标签: c# arrays json unity3d


    【解决方案1】:

    最简单和最有效的方法是将所有数据都设为同一类型。目前,您似乎认为每个对象都是给定 id 的类型:

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

    如果尝试使用可序列化的类,这不会太顺利。

    我会推荐以下内容:

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

    然后您可以轻松地序列化/反序列化

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

    然后在 Unity 中:

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

    如果您想加快获取速度并且您的集合很大,请转换为字典。

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

    现在您可以真正快速访问了。

    Item GetItem(string id)
    {
         if(string.IsNullOrEmpty(id) == true){ return null; }
         Item item = null;
         this.dict.TryGetValue(id, out item);
         return item;
    }
    

    【讨论】:

      【解决方案2】:

      如果您最终在文件中存储了数百万条记录并想要开始做一些性能更高的事情,那么切换到像 MongoDB 这样的体面的文档数据库会更容易,而不是尝试重新发明轮子。

      在担心尚不存在的性能问题之前先担心编写好的标准代码。

      以下示例未使用您选择的语言,但它确实说明了可以非常快速地搜索 JSON 和 1,000,000 个对象的数组:

      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)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-15
        • 1970-01-01
        • 2021-11-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多