Json搜索数据结构

时间:2015-11-19 02:49:30

标签: c# json unity3d

这是我的数据结构,我正在尝试使用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;                                         
}

1 个答案:

答案 0 :(得分:1)

您将项目视为对象而非数组。

我不知道您使用什么JSON库来为您提供指导,但这样的事情可能会有效:

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