使用Node.js从Json文件解析嵌套对象

时间:2020-07-01 21:33:54

标签: javascript arrays json

我有一个Json文件,它实际上是一个2D数组。该数组包含Discord Server ID的数组。这些服务器ID数组包含用户ID。用户ID对象包含与Discord机器人相关的数据。

Foo?

我想做什么以及为什么不起作用:

我正在尝试从User对象检索数据。问题是,我只能返回“未定义”或 当我甚至尝试访问ServerID数组时,“对象对象”,更不用说用户数组了。 这是我到目前为止的代码。 (对于Javascrips来说我还很陌生,所以请问一些愚蠢的问题吧)

{
    "Servers":[
        {
            "724992148444938330":[ //This is a Server ID
                {
                    "110596839018856448":{ //This is a UserID
                        "userID":"110596839018856448",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                    "110596839018856449":{
                        "userID":"110596839018856449",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                "110596839018856450":{
                        "userID":"110596839018856450",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                }
            ]
        },
        {    
            "724992148444938331":[
                {
                    "110596839018856448":{
                        "userID":"110596839018856448",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                    "110596839018856449":{
                        "userID":"110596839018856449",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                "110596839018856450":{
                        "userID":"110596839018856450",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                }
            ]
        },
        {
            "724992148444938332":[
                {
                    "110596839018856448":{
                        "userID":"110596839018856448",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                    "110596839018856449":{
                        "userID":"110596839018856449",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                "110596839018856450":{
                        "userID":"110596839018856450",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                }
            ]
        }
    ]
}

我希望它执行/返回的操作:

我想获取特定的数据集,具体取决于服务器ID和用户ID是什么。这意味着如果我想获取CurrentChar,id喜欢按照

的方式进行操作
    function getCurrentCharName(serverID,userID){  //retrieve current char name from user ID and return it
    serverID = "724992148444938330" //Server ID im trying to address
    userID = "110596839018856448"  //User im trying to address
    var userPath = "./Resources/UserData/user.json" //path of json File
    var Data = fs.readFileSync(userPath);
    Data = JSON.parse(Data);

    console.log(Data.Servers.find(itm => itm == 724992148444938330)) //returns 'undefined'
    console.log(Data.Servers.724992148444938330); //doesnt allow that
    console.log(Data.Servers.serverID); //returns undefined


    for(var itm in Data.Servers){ //returns '[object Object]' 
        console.log(itm + ": " + Data.Servers[itm]);
    }
    };

这应该返回字符串'samplechar.json。

就像我说的那样,我是Javascript和Json的新手,所以这里可能存在一个非常明显的缺陷,但对此我要感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

7 ─ %15 = Base.getindex(Main.ref)::Int64

您要让JS在这里执行的操作基本上是逐一遍历数组,然后查看该数组索引是否等于'724992148444938330'。但是,它看起来像这样

Data.Servers.find(itm => itm == 724992148444938330)

因此 { "724992148444938330":[ //This is a Server ID { "110596839018856448":{ //This is a UserID "userID":"110596839018856448", "CurrentChar":"samplechar.json", "CurrentCharName":"Bob", "CurrentGame":"StructureIdea.json" } }, { "110596839018856449":{ "userID":"110596839018856449", "CurrentChar":"samplechar.json", "CurrentCharName":"Bob", "CurrentGame":"StructureIdea.json" } }, { "110596839018856450":{ "userID":"110596839018856450", "CurrentChar":"samplechar.json", "CurrentCharName":"Bob", "CurrentGame":"StructureIdea.json" } } ] } 将永远不会过去。相反,应该做的是这样的:

itm == 724992148444938330 

服务器现在应为以下数组:

   let servers = Object.entries(Data.Servers);

对象的键存储在索引0中,值存储在索引1中。因此,现在要查找服务器'724992148444938330',您可以执行以下操作:

    [ 
        ['724992148444938330', [the value of '724992148444938330']
    ]

或者您可以更具体一些,并执行以下操作:

let serverNeeded = servers.find(itm => itm[0] == 724992148444938330)

无论哪种情况,serverNeeded现在都应该等于该数组:

let serverNeeded = servers[0][0][1]

要解析这一点,我们需要遵循之前所做的相同方法:

            [ //This is a Server ID
                {
                    "110596839018856448":{ //This is a UserID
                        "userID":"110596839018856448",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                    "110596839018856449":{
                        "userID":"110596839018856449",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                "110596839018856450":{
                        "userID":"110596839018856450",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                }
            ]

我确定您可以提出一种自动执行此操作的方法。 另外,如果您想了解有关Object.entries()的更多信息,那么这是一个很好的资源:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

此网站上也提供了许多其他有用的方法。

答案 1 :(得分:0)

我相信您正在寻找的是:

console.log( Data.Servers.find(itm => Object.keys(itm).includes("724992148444938330")) );

其输出将类似于:

{ 
    '724992148444938330': [{ 
        '110596839018856448': <Object> 
    }, { 
        '110596839018856449': <Object> 
    }, { 
        '110596839018856450': <Object> 
    }] 
}

以同样的方式,您还可以像这样进一步深入:

console.log( Data.Servers.find(itm => Object.keys(itm).includes("724992148444938330"))["724992148444938330"].find(usr => Object.keys(usr).includes('110596839018856448')) );

输出:

{ 
    '110596839018856448': { 
        userID: '110596839018856448',
        CurrentChar: 'samplechar.json',
        CurrentCharName: 'Bob',
        CurrentGame: 'StructureIdea.json' 
    } 
}