【问题标题】:Json Searching Data StructuresJson 搜索数据结构
【发布时间】:2015-11-19 02:49:29
【问题描述】:

这是我的数据结构,我正在尝试使用 simpleJason 通过 unity/c# 访问它,我不小心在这里和那里得到了正确的数据,否则得到完全空的数组,我想知道我的JSON 文件未正确设置我的数据结构,或者解析器是否以某种方式下降,或者与我要查找的内容不正确匹配。

JSON 文件:

{
    "categories": [
        {
        "name" : "entertainment",
            "projects": [
                {
                "name": "Awards",
                "description": "Awards Shows",
                "credits": [
                    "Lead Engineer - Dave Jones",
                    "VFX Supervisor - John Adrian",
                    "CG Supervisor - Evan Klein"
                ],
                "meta": [
                    "awards",
                    "show",
                    "stars",
                    "red carpet"
                ],
                    "assets": [
                        {
                            "name": "Screen Actors Guild Awards",
                            "filename": "SAG_Awards.mp4",
                            "icon": "sag.png",
                            "stereo": false,
                            "meta": [
                                "Screen",
                                "Actors",
                                "Guild"
                            ]
                        },
                        {
                            "name": "No Awards",
                            "filename": "No_SAG_Awards.mp4",
                            "icon": "no_sag.png",
                            "stereo": false,
                            "meta": [
                                "Screen",
                                "Actors",
                                "Guild"
                            ]
                        },
                        {
                            "name": "None Awds",
                            "filename": "None_SAG_Awards.mp4",
                            "icon": "none_sag.png",
                            "stereo": false,
                            "meta": [
                                "Screen",
                                "Actors",
                                "Guild"
                            ]
                        }
                    ]
                }
            ]
        }
    ] 
}

这是结构:

private struct jsonAsset {
        public string category;
        public string project;
        public string description;
        public string[] credits;
        public string[] meta;
        public string asset;
        public FileInfo file;
        public FileInfo icon;
        public bool stereo;
        public bool overUnder;
    };

这是函数:

jsonAsset LoadSceneDataFromJSON(FileInfo jsonFile)
{
    jsonAsset asset = new jsonAsset(); 

    Debug.Log("Processing : " + jsonFile);

    // Parse File for Data
    var N = JSON.Parse(File.ReadAllText(jsonFile.FullName));
    var cat_arr = N["categories"].AsArray;
    asset.category = N["categories"]["name"].Value;
    Debug.Log(N["categories"]["projects"]["assets"]["filename"].Value);
    foreach (JSONNode n in cat_arr)
    {            
        asset.project = n["name"].Value;

        // Credits
        var proj_credits = n["credits"].AsArray;                   
        foreach (JSONNode pc in proj_credits)
        {
            asset.credits[asset.credits.Length] = pc["credits"].Value;
        }

        // Project Meta
        var proj_meta = n["meta"].AsArray;
        foreach (JSONNode pm in proj_meta)
        {
            asset.meta[asset.meta.Length] = pm["meta"].Value;                
        }

        // Project Array
        var proj_arr = n["projects"].AsArray;
        foreach (JSONNode nn in proj_arr)
        {
            var asset_arr = nn["assets"].AsArray;
            asset.asset = nn["assets"]["name"].Value;
            foreach (JSONNode nnn in asset_arr)
            {
                asset.asset = nnn["name"].Value;
                asset.file = new FileInfo(m_dir + nnn["filename"].Value);
                asset.icon = new FileInfo(m_dir + nnn["icon"].Value);
                var asset_meta = nnn["meta"].AsArray;
                foreach (JSONNode am in asset_meta)
                {
                    asset.meta[asset.meta.Length] = am.Value;
                }
            }
        }
    }
    return asset;                                         
}

【问题讨论】:

  • N 在 JSON.Parse 被调用后是什么样子的?另外,您是否考虑过为 C# 使用 Newtonsoft 的 JSON 解析器?
  • 如果要检查 JSON 文件是否有效,可以使用 jsonlint.com。顺便说一句,您的文件是有效的。

标签: c# json unity3d


【解决方案1】:

您将 projects 视为对象而不是数组。

我不知道您使用什么 JSON 库来为您提供指导,但这样的东西可能会起作用:

 N["categories"]["projects"][0]["assets"]["filename"].Value

【讨论】:

  • 我希望所有类别、项目和资产都是数组,我认为 "projects": [ { 做到了。因为我想允许多个类别、项目或资产在 .json 文件中可用或在文件中分布 (1/.mp4)
猜你喜欢
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
相关资源
最近更新 更多