Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        2年前关闭。
                    
                
        

我有一个如下的json文件:


{ "userdata": { "userid123": {"uname": " john", "uemail": "[email protected]"}, "userid124": {"uname": "sam", "uemail": "[email protected]"} }

如果有人进入URL http://example.com/?userid123,我想解析用户详细信息

因此,仅显示userid123的详细信息(位于url中,从那里获取它)。

最佳答案

您添加到问题的Json字符串是错误的。

如果需要在问号后获取字符串并测试是否存在相应的值,可以编写:



var js = {
    "userdata": {
        "userid123": {"uname": " john", "uemail": "[email protected]"},
        "userid124": {"uname": "sam", "uemail": "[email protected]"}
    }
};

var sp = window.location.search.substr(1);

//for testing purposes
sp='userid123';

if (js.userdata[sp] !== undefined) {
    console.log('uname: ' + js.userdata[sp].uname  + ' uemail: ' + js.userdata[sp].uemail)
}

07-24 15:55